This site requires JavaScript, please enable it in your browser!
Greenfoot back
RcCookie
RcCookie wrote ...

2021/1/8

Get weather the act loop is running

RcCookie RcCookie

2021/1/8

#
Is there a way to find out weather the act loop is currently running? I'm trying to create applications in Greenfoot and would like to run each application on a different thread. To prevent them from running even if you pause the scenario I need to figure out weather it should actually run or not. I have tried to create a new frame each act iteration that only calles the act method of the application, but the performance was really bad.
danpost danpost

2021/1/8

#
RcCookie wrote...
Is there a way to find out weather the act loop is currently running?
Only the active world and its actors will run at any time (while the scenario is in a running state).
I'm trying to create applications in Greenfoot and would like to run each application on a different thread.
Are you talking multiple applications in one scenario? Are you using the java.lang.Thread class? (you should not be)
To prevent them from running even if you pause the scenario
??? They won't run if you pause the scenario.
I need to figure out weather it should actually run or not.
Are you just trying to control which world is active?
I have tried to create a new frame each act iteration that only calles the act method of the application, but the performance was really bad.
Without the code, it is hard to understand exactly what you tried.
RcCookie RcCookie

2021/1/10

#
So yes, I am using java.lang.Thread, and I am fully aware of the fact that that is not gonna work in the online conversion. What I’ve tried:
// In World subclass
public void act() {
    Widget w = getWidget();
    new Tread(() -> w.run()).start();
}
This however is really bad for performance, which was to be expected. The idea:
// In World subclass
private boolean started = false;

@Override
public void started() {
    if(started) return;
    started = true;
    Widget w = getWidget();
    new Thread(() -> {
        while(true) {
            w.run();
        }
    ).start();
}
I am aware that this way the update rate will be limited only by the cpu, but that’s fine. The real problem is that this thread will in fact keep running even if the scenario gets paused. So the idea is:
// In World subclass
private boolean started = false;

@Override
public void started() {
    if(started) return;
    started = true;
    Widget w = getWidget();
    new Thread(() -> {
        while(true) {
            if(actLoopActive()) w.run();
        }
    ).start();
}

private boolean actLoopActive() {
    // Some way of figuring out if the act loop is currently running
}
RcCookie RcCookie

2021/1/10

#
The point of this is to prevent lock-ups if there are multiple apps running concurrently and one is stuck or slowing down the framerate a lot
RcCookie RcCookie

2021/1/10

#
Btw why should I not use Thread? Except it not working online?
danpost danpost

2021/1/10

#
RcCookie wrote...
this thread will in fact keep running even if the scenario gets paused.
Use the World class stopped method to stop any running threads. There is a started method, also (if needed).
You need to login to post a reply.