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
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