-
interrupting threads
The documentation is confusing me on this.
What I want is having a thread sleep until I give it a GO signal. How would I do this? I have looked into sleep() and interrupt(), but I don't really understand it. What does the "interrupted" state mean? Is it related to the running state of the thread? If yes, how? If no, how do I get the running state?
I want to be able to stop the thread again with another signal. How would I do that? suspend, stop etc. are deprecated.
Thanks in advance
-
I now use a signaling model with Object.wait() and Object.notify(), but I don't undestand their documentation either, so it might not work. Especially the parts about monitor ownership.
On a related note, is there something like a critical section in Java? I have several threads, and most of them might want to access one global java.awt.Robot object, I have to synchronize the access.
Here's the signaling code I currently use:
Code:
class SomeClass implements Runnable
{
private Object startSignal;
private boolean running;
private Thread solver;
public SomeClass()
{
startSignal = new Object();
solver = new Thread(this);
setRunning(false);
solver.start();
}
public void startstop()
{
if(isRunning()) {
setRunning(false);
} else {
setRunning(true);
startSignal.notify();
}
}
synchronized boolean isRunning()
{
return running;
}
synchronized void setRunning(boolean r)
{
running = r;
}
// this might cause huge synchronization problems...
public void run()
{
while(true) {
startSignal.wait();
while(isRunning()) {
internalStep();
}
}
}
}
internalStep does calculations.
-
The problem is that there might (and will) be several objects of SomeClass, each having it's own thread trying to access the same java.awt.Robot
-
http://java.sun.com/docs/books/tutor...o/threads.html
Any use? Theres some stuff on synchronising blocks etc.
HD
-
From what i remember of multithreadding the sleep method is used to put the thread to sleep for a period of time specified by the arguement passed to the method.
Calling wait() causes the current thread to block until the objects notify() method is called by another thread.
-
Dave: seems interesting, thanks.
Dilenger: yeah, that's about the part of the docs I understood . Nothing else.
-
Dave: just found out it's exactly what I needed. Double thanks. :)