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
Knute Johnson - 18 Nov 2005 00:41 GMT
> 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()
[quoted text clipped - 3 lines]
>
> Joshua McGee
Look at java.util.Timer.scheduleAtFixedRate()

Signature
Knute Johnson
email s/nospam/knute/
jigounov@gmail.com - 18 Nov 2005 02:58 GMT
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.
Roedy Green - 18 Nov 2005 14:24 GMT
>Any help would be appreciated.
If you just want to get it going, use a timer instead. See
http://mindprod.com/jgloss/timer.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
joshuamcgee@gmail.com - 21 Nov 2005 04:17 GMT
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