Dave
Originally posted by Dave Vick:
Instead of testing useing join you should use isAlive(), it returns a boolean telling if the thread has started but has not completed its run method yet. 2001).]
Dave
Sun Certified Programmer for the Java 2 Platform
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Peter den Haan:
Well, I'm not sure if it is your problem or not, but it is certainly a source of JVM-dependent bugs: access to isStopped should be synchronized, or alternatively isStopped needs to be declared volatile.
Generally, a debugger or good old System.out.println will tell you whether the problem is that join() doesn't work or simply that the threads don't stop.
Finally, what was wrong with interrupt()? You're reinventing the wheel here (and paying for it from the looks of it).
- Peter
Unless you use either, there is no guarantee that your code in the thread ever gets to see a modified value of isStopped - the variable might be held in a register and never re-read from memory. Again I'm not sure how theoretical this point is.Originally posted by Francisco Iacobelli:
What would be the benefits of synchronize or volatile?
Did you take into account that Thread.interrupted() actually clears the flag? If you'd do something likeit will not work. My preferred method of handling interrupts is simply stick inside a reasonable inner loop:and handle cleanup etc. in finally clauses. Typically the interrupt is caught only at the outermost level in the run() method.Oh, instead of isStopped, I had t.interrupted(), and I had used interrupt, but that didn't work at all. I would interrupt the thread, but the while loop, for some reason I don't know, would never exit. Any ideas?
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Francisco Iacobelli:
Hi,
I am dealing with threads. I have an ArrayList of threads which I am trying to stop. The run() method in the threads is a while loop.. i.e. while(isRunning){ do something }
Now, I set this "isRunning" variable to false when I want to stop the thread. It exits the run method, but the thread is still alive. I use the join() method to make sure it has died, but it hangs on the join(). What can be keeping the threads from dying?. being dead and stopped are different?
thanks!
Francisco
Originally posted by CL Gilbert:
[B]
try seeing what happens if you say this
you will see that this while loop will eventually quickly exit, but it will cycle a few times.[/B]
This guy is skipping without a rope. At least, that's what this tiny ad said:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|