aspose file tools*
The moose likes Threads and Synchronization and the fly likes interrupt() without sleep() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "interrupt() without sleep()" Watch "interrupt() without sleep()" New topic
Author

interrupt() without sleep()

olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
i have a thread which searches some things in the internet... this sometimes take a while and i want the user to stop that thread
i used .stop() (i know its bad ) and now i want to change that to something like interrupt() and catch the InterruptedException
but i dont use any method that throws this exception, and its not a loop (otherwise i would put a sleep(1) in it so i can catch the exception)
can someone tell me how to do this?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

Basically, when you call interrupt() and...

1. In a sleep(), wait(), join(), etc., it will throw an InterruptedException.

2. Doing IO, it may throw an InterruptedIOException. This is only thrown if interrupted IO is supported... And the most common OS, Windows, does not support this exception.

3. Doing anything else, it will simply set the interrupted flag.

An InterruptedIOException IS-A IOException. And when you close the resource, file or socket, an IO exception will be thrown -- if it is attempted to be used later, or is in use by another thread.


So how do you do this?


In the thread to be interrupted...

1. Before calling sleep(), wait(), or any IO operation, check the interrupted flag. And if true, jump to the code that parses the interrupt.

2. With long lengths of processing, check the interrupted flag once in a while. And if true, jump to the code that parses the interrupt.

3. Catch the InterruptedException, where one is thrown. And jump to the code that parses the interrupt.

4. Catch the IOException, where one is thrown. And if related to interrupt, jump to the code that parses the interrupt.


In the thread that wants to do the interrupt.

1. Call interrupt() on the thread to be interrupted.

2. Close any resources that the thread to be interrupted may be using that will no longer be needed. This is needed to get your thread out of IO, if InterruptedIOException is not supported.

3. Set any variables that will help that thread to be interrupted to determine the status. This is needed if there are other valid IO Exceptions, and the thread needs to determine if it is caused by an interrupt or not.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

olze oli wrote:
i used .stop() (i know its bad ) and now i want to change that to something like interrupt() and catch the InterruptedException


Since you used the "roll eyes" emoticon, I am thinking that you are not a believer that the stop() method should have been deprecated? Maybe I am wrong, but here is the explanation regardless...


When the stop() method was deprecated, it was due to a really really obscure race condition that could not be fixed. It was possible that the stopping thread could unroll the stack (with the ThreadDeath object), in a way that put internal JVM flags in a useless state. This generally could cause the JVM to crash, or just not function.

Over the years, since the method has been deprecated, developers have tested this case less and less. After all, why test it? The stop() method is not supposed to be ever called right? Because of this, this condition has become more and more common -- particularly when using debuggers and IDEs.

Henry
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
i know its bad and the rolleyes signalize my shame on this ;)

ok then i will implement a few if's which check if a boolean flag has been set and return if it is set
i thought there is maybe a better way doing this
 
 
subject: interrupt() without sleep()
 
Similar Threads
Can one thread halt another thread?
Stopping a thread via command line
Thread
Database Thread Safety, pls review
best way to kill ?