Shutdown of scheduled threads?

Im following the multithreading example with a twist, where it runs on a delay;

[java] final ScheduledFuture<?> dayNightHnadler = executor.scheduleWithFixedDelay(dayNight, 0, 5, TimeUnit.SECONDS);

[/java]

*this is not within the update loop (should it be ? ), the dayNight simply moves the position of the lamp (sun)

the problem is how do i shutdown this thread when the game closes ?

You always just need to shut down the executor, not single tasks.

hmm now i get, java.lang.IllegalStateException: Scene graph is not properly updated for rendering.

i imagine thats because im trying to call the updateSunPosition method from a different thread, is it possible have a delayed thread within update loop ?

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:multithreading

yeah sorry i fixed that exception problem, i think the what im trying to say is where is shutdown put, right now the executor is started in simpleInitApp() im gonna try the top most main, thanks for the help, i jumped to the forums too quickly

Just override the destroy method of the application:

[java] @Override

public void destroy() {

executor.shutdown();

super.destroy();

}

[/java]



Actually the best way would be to have an AppState which can then be accessed by any part of the software via the Application class. Then in its destroy() method, you shutdown the executor.

aha of course, i was looking for app.isDestroyed, thanks Normen, im going to try and use the mutlithreading example in tutorials another way, one which relies on state rather than delay

1 Like