aspose file tools
The moose likes Threads and Synchronization and the fly likes Innterupted before waiting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Innterupted before waiting" Watch "Innterupted before waiting" New topic
Author

Innterupted before waiting

Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 912
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.


SCJP, SCJD, SCWCD
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 288

Your answer is in the javadoc for Thread.interrupt().


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 912
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

Joined: Sep 19, 2005
Posts: 288

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:
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Innterupted before waiting
 
Similar Threads
What causes InterruptedException? How to recover?
INTERRUPTEDEXCEPTION
Threads join() method
wait and notify
threads