aspose file tools*
The moose likes Threads and Synchronization and the fly likes I dont understand Java's workaround for stopping a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "I dont understand Java Watch "I dont understand Java New topic
Author

I dont understand Java's workaround for stopping a thread

Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
HI Everyone,

According to this article http://docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html it says to perform the following as a mechanism for stopping a thread of execution




how is calling stop() method on this ensuring that the currently running thread of execution stops? what i mean is lets say repaint()method is already invoked. while repaint() method is executing , if i call sstop() according to me nothing will happen. repaint method will still complete execution. the above code ensures that the next time when this thread is picked up for execution, it will check for the stop flag.

AM i wrong in my understanding? please help
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

I think the code is a little bit incomplete, but assuming that I'm right about what you didn't post...

Yes, your understanding is mostly correct. The "stop" method will not stop the thread instantly, it will just cause the thread to end when it's ready to end. However this part:

the above code ensures that the next time when this thread is picked up for execution, it will check for the stop flag.


I don't think that's correct. Although it really depends on what you mean by "picked up for execution". The Thread you've posted there runs an infinite loop which alternates between sleeping, repainting, and checking whether "stop" has been called. So after you call its "stop" method, it might still sleep for a while. It might repaint again after that. But then it will notice that the condition for the while-loop is no longer true, and it will exit from the "run" method. This causes the Thread to end.
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
thank you paul.

lets suppose i tweak the requirement a bit. lets say my run() method needs to call repaint() only once. in such a case, how do i stop a thread from completing the repaint() method

eg given below

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

You don't. If you want the repaint() method to be interruptible, you have to write it that way. It just isn't possible for Java to interrupt arbitrary code and have it return from a method, abnormally or otherwise.
Stanley Walker
Ranch Hand

Joined: Sep 23, 2009
Posts: 87
It just isn't possible for Java to interrupt arbitrary code and have it return from a method, abnormally or otherwise.

I was thinking the same way, but numerous google searches on the internet on the same topic made me think that it might be possible. Thank you for the clarification, Paul
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16246
    
  21

In the original Java, it was possible for an external source (another thread) to forcibly stop a thread.

This was determined to be a bad idea, since a thread could be halted while in the middle of a sensitive operation, while holding locks (synchronized code), and/or while holding resources that needed to be properly freed.

So support for that type of operation was withdrawn in favor of a convention where a thread would check for external notification that it should halt itself (after first properly cleaning up).

Even full-blown operating systems, which often have extensive built-in resource management, termination and recovery facilities have occasional problems with thread/process breakage, as can be seen with the Linux "Zombie" process status, so Java avoided all the complications by insisting on use of a simpler model.


Customer surveys are for companies who didn't pay proper attention to begin with.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 543
    
    4

The idea here is just that the thread runs its due course. The best method of being able to stop/interrupt a thread, is writing your code to check whether it is allowed to continue before it continues.

So if you're writing code where you need to call a Thread's API to interrupt or stop it, you're going about it the wrong way.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I dont understand Java's workaround for stopping a thread