"Proper way" to let threads sleep or to create timed threads?

This is not realy jm3 related but java in general.
I wonder a while now how this can be done different or more “proper”, because i read a lot that it is not good practice to use sleep and yield in a thread. Also NetBeans started to complain using sleep a few versions ago.

Usualy i do something like this when i need something to run say every minute (very basic example):
[java] private class TimedWorker extends Thread {

    private boolean running = true;
    private int timeframe = 60000;
    private int timer = 60000;
    
    @Override
    public void run() {
        while (running) {
            // do something here
            
            while (running && timer > 0) {
                try {
                    sleep(1);
                } catch (InterruptedException ex) {
                }
                timer--;
            }
            timer = timeframe;
        }
    }
}[/java]

I know this ignores the time of the actual work being done in the thread too. Could get the current tick from the current time and use that to find the next minute but i don’t like getting the current time too often because it also creates a date object, or maybe i miss something ?

Running a thread to let it sleep makes no sense at all. If you want to do an action without blocking the update loop but after a certain time, just use a timer.
[java]time = time +tpf;
if(timer>3000){
doStuff();
}[/java]
Otherwise use an executor as outlined in the threading documentation (https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:multithreading) to only spawn a thread for the actual work when its due. If you really want a thread to wait use the wait/notify/join model in java.

Generally I think that you should probably learn more about threading given your approach as you are probably more in for headaches than a performance gain :wink:

I’m almost inclined to tell people: Learn java.util.concurrent or don’t do threading.

The threading constructs you are using are 15 years old or so. There is much newer and better stuff. In this case, look at the scheduled thread pool executor. Or take a look at this factory class: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Executors.html

Edit: or as normen hints, examine whether you really want a background thread or are you just using a steam roller to kill a fly?

I wasn’t talking about the update loop but threading in general.
I haven’t looked into threading for a long time, so was not sure what kind of newer and better stuff exists by now.
Might not have been the best place to ask here because it is a general java question :wink:
I just meant how to create a thread that does something every x seconds. As it does the same thing every time i thought spawning new threads is not neccesary and i can just loop in the thread and let it sleep for a while.
As pspeed said this is a realy old approach, just wasn’t sure what else exists by now.

Look at the timer class, it does exactly what you need. Else than that the sleep is an acceptable solution, if it is not in a rneder thread (swing,jfx,jme,opengl ect)

@ryukajiya said: I wasn't talking about the update loop but threading in general. I haven't looked into threading for a long time, so was not sure what kind of newer and better stuff exists by now. Might not have been the best place to ask here because it is a general java question ;) I just meant how to create a thread that does something every x seconds. As it does the same thing every time i thought spawning new threads is not neccesary and i can just loop in the thread and let it sleep for a while. As pspeed said this is a realy old approach, just wasn't sure what else exists by now.

Yeah but that approach isn’t being used for anything but hardware or os-level services anymore really, in any language. Thats why I was talking about upping your multithreading skills and approach. Look at the concurrency API for the base of a scheme thats at least still being used and not ages old :wink:

Thanks for pointing me in the right direction, that was exactly what i was looking for :wink:
Allready looking through that api. Some areas of my java voodoo have become a bit rusty it seems.

Isn’t it possible to mark a thread as resolved any more ? I don’t see the checkbox.

@ryukajiya said: Thanks for pointing me in the right direction, that was exactly what i was looking for ;) Allready looking through that api. Some areas of my java voodoo have become a bit rusty it seems.

Isn’t it possible to mark a thread as resolved any more ? I don’t see the checkbox.

Again, you don’t manage the single threads yourself, you use an executor.

Duh, for this forum? Only if its a support thread in the first place you have a dropdown on top.

<cite>@Empire Phoenix said:</cite> Look at the timer class, it does exactly what you need. Else than that the sleep is an acceptable solution, if it is not in a rneder thread (swing,jfx,jme,opengl ect)

The timer class is broken tbh, I really wouldn’t recommend it.

(Well, it works - but the API is clumsy and there are a lot of annoying limitations).

Use a ScheduledExecutorService.