Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interrupt() without sleep()

 
olze oli
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic