Custom Java sleeper thread not triggering reliably

J

joshuamcgee

Hello all,

I needed a way for my class to sleep for a specified amount of time,
then wake up and perform an operation, in a non-blocking fashion. I
came up with the following:

//************************************************************
private class SleeperThread extends Thread {
long _id;
long _sleepTo;
Executive _parent;
SleeperThread(long sleepTo, long id, Executive parent) {
_id = id;
_sleepTo = sleepTo;
_parent = parent;
}

public synchronized void run() {
long interval = _sleepTo - time();
if (interval > 0) {
try {
wait(interval);
}
catch (InterruptedException ex) {
System.out.println("Sleep interrupted");
return;
}
}
_parent.timeTrigger(_id, _sleepTo);
}
}
//************************************************************

This class is constructed with a long representing the time in epoch
millis to sleep until, a long timer ID, and a pointer to its parent.
The run method waits for the desired duration, then calls a method in
the parent called "timeTrigger". It would be invoked like this:

//************************************************************
SleeperThread sleeperThread = new SleeperThread(time, sleeperID,
this);
sleeperThread.start();
//************************************************************

This seemed like an elegant solution, and it works sometimes, but other
times timeTrigger is never called, in an unpredictable fashion. It is
hard to debug the multi-thread behavior in JBuilder.

Am I making an invalid assumption? Have I made a coding blunder? Is
there a built-in way to do this? Note that I cannot just call wait()
in the main thread, because that would block the thread's execution.

Any help would be appreciated.

Joshua McGee
 
K

Knute Johnson

Am I making an invalid assumption? Have I made a coding blunder? Is
there a built-in way to do this? Note that I cannot just call wait()
in the main thread, because that would block the thread's execution.

Any help would be appreciated.

Joshua McGee

Look at java.util.Timer.scheduleAtFixedRate()
 
J

jigounov

1. replace wait() with Thread.sleep()

2. Depending on what call _parent.timeTrigger(_id, _sleepTo); does you
might want to call it outside of synchronized block:


public void run() {
....
synchronized(this) {
try {
Thread.sleep()
} catch(...) {
...
}
}
_parent.timeTrigger(_id, _sleepTo);
}

Good luck.
 
J

joshuamcgee

Thank you, that's very much like what I was trying to duplicate. I was
initially worried about tasks bunching due to slowish execution, but I
think all the tasks will be rather quick (just sending a UDP packet to
another component.)

Thank you to the other two who advised. I'll post again if anything
interesting develops.

Best,

Joshua McGee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top