|
-
Nov 8th, 2002, 09:39 AM
#1
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 10:10 AM
#2
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 10:11 AM
#3
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 10:45 AM
#4
Addicted Member
http://java.sun.com/docs/books/tutor...o/threads.html
Any use? Theres some stuff on synchronising blocks etc.
HD
-
Nov 8th, 2002, 04:05 PM
#5
Dazed Member
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.
-
Nov 8th, 2002, 08:07 PM
#6
Dave: seems interesting, thanks.
Dilenger: yeah, that's about the part of the docs I understood . Nothing else.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 8th, 2002, 08:10 PM
#7
Dave: just found out it's exactly what I needed. Double thanks.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|