• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interrupt

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all...
I am not able to understand the below program,especially the purpose of interrupt().
When a thread calls wait() it is put in waiting state.
So without a notify() or notifyAll() how is the thread able to come out of the wait state.

This program give an output of false.
can anybody explain me in detail.
Thanks
[ February 14, 2005: Message edited by: Jim Yingst ]
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So without a notify() or notifyAll() how is the thread able to come out of the wait state.


... or if you interrupt it, which you aren't doing in all cases.
You have demonstrated a case of the "lost interrupt".
Your code is not guaranteed to always acknowledge the interrupt during the call to wait() and thus deadlock.

Have you ever used an application that deadlocked, rang customer support only to be told that they cannot reproduce it under any conditions and so you must be making it up?

Formatted:

[ February 14, 2005: Message edited by: Tony Morris ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason the output is false is given in the API for the wait() method:

"Throws: ... InterruptedException - if another thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown."

What this means is that after an InterruptedException has been thrown, interrupted() will return false. If you'd been able to invoke this method before the exception was thrown, from within the interrupted thread, you would have seen a true instead. This is only an issue if you're not calling a method like wait() or sleep(), which are required to detect interruptions for you and respond by throwing InterruptedExceptions. If you're doing some other time-consuming activity, and want to check if there have been interruptions, that's what interrupted() (and isInterrupted()) are for.

[Tony]: Your code is not guaranteed to always acknowledge the interrupt during the call to wait() and thus deadlock.

Mmmm, I'm not seeing the loophole here. Are you referring to the possibility that interrupt() might be called before the wait() is executed? Not a problem - wait(), like sleep(), checks for any uncleared interruptions that occurred beforehand, and responds by throwing InterruptedException when appropriate. E.g.:

Note that I replaced interrupted() with isInterrupted() to avoid changing the result on subsequent calls.

In the event I've missed my guess and Tony is actually talking about something else, hopefully the above code will be of interest to Ramya at least.
[ February 15, 2005: Message edited by: Jim Yingst ]
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim...
I got an idea what interrupted() and isInterrupted() methods does.
I am not clear what a1.interrupt() does.To know that i just removed a1.interrupt() when i did that i got the following output for your modified program.

sum:49995000
before wait:false
after this the program doesn't shows any result.

But the actual output without removing a1.interrupt() is
sum:49995000
before wait:true
inside catch:false.

can you make this clear to me.
[ February 15, 2005: Message edited by: ramya jp ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend adding more print statements before & after every line you're unsure of. That way you can see exactly what has executed, and what happens. For example:

Study the output carefully; you should be able to determine what is happening.
[ February 15, 2005: Message edited by: Jim Yingst ]
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jim i happen to execute the program with print statements.
I analysed that if the interrupt() is removed then the run() method is not called immediately and once it encounters wait()InterruptedException is not caught and the program doesnt get completed.

When we dont remove the interrupt(), this is how the code looks like..

The output is

main 1
main 2
main 3
main 4
first line in run
second line in run
before wait :true
third line before while
inside the while
inside the catch
false.



I then modified the program by removing the interrupt () and using the notify().

the output of this program is

main 1
main 2
main 3
first line in run
second line in run
before wait:false
third line before the while
inside the while
notification is going on
fourth line
main 4.

After this the program end.

So the difference i found out is when interrupt() is invoked the catch clause catching the exception and then the program ends.

When notify is used the catch clause is not executed and the program ends without any issues.

I dont know whether i am right .Kindly correct me if i have understood the concept wrongly.

Thanks in advance.

[ February 19, 2005: Message edited by: ramya jp ]

[ February 19, 2005: Message edited by: ramya jp ]
[ February 19, 2005: Message edited by: ramya jp ]
 
I don't always make ads but when I do they're tiny
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic