how to end a thread when it is in sleep?
i am making a project in which i ahve shedule somthing happen after a certain period of time, i calculate that time an pass that time in sleep method. if i again changes the time then i want to end previous thread and start a new thread. so i want to know how to end the thread which is in sleep method without executing it开发者_运维知识库's run method.
I assume Java when you are programing for Blackberry?
If yes, don't use Thread.sleep()
to wait, but instead wait()
on a monitor, which you can then notifyAll()
, i.e.
private Object waitObject = new Object();
public void doWait() {
synchronized (waitObject) {
waitObject.wait(10*1000); // wait up to 10 seconds
}
}
public void wakeUp() {
synchronized (waitObject) {
waitObject.notifyAll();
}
}
The synchronized
blocks are important, as you need to own the monitor you want to wait on or notify on.
精彩评论