Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Innterupted before waiting  RSS feed

 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I have a dilemma. As far as I know a thread can be interrupted (with InterruptedException) only if it waits (wait,sleep, IO operations), but according to the sun API for the Object :

InterruptedException - if another thread interrupted the current thread before or while the current thread was waiting for a notification.


What I don't understand is the "before" wait statement.
How can a thread be interrrupted before it waits ?

Thanks Mihai.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your answer is in the javadoc for Thread.interrupt().
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have something that might fit that description. It starts a worker thread to do a couple long-running operations, but it only waits so long for it to finish. If it runs too long we interrupt it and go on my way. The worker thread never does any thread wait.

I wonder now if I ever tested to see if the interrupt does anything. What do you think?
[ October 05, 2006: Message edited by: Stan James ]
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

and thanks for your fast answers.

Edward,
Ok I agree with you, the documentation for Thread is ok but the one from the Object ? From here I extract the "before" stuff.

Stan,
If the "work thread does not waits" interrupt has no visible effect - it only set the interrupted flag, no exception(InterrupterdException) is throw.

Regards M.
 
Edward Harned
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All interrupt() does is set a flag. The thread then checks if it isInterrupted(). So the thread is not "interrupted" before it waits.

Personally, I never use this feature. When you wait() you do so in a loop testing for completion of the work the other thread is supposed to do:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!