File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Why send interrupt to a thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why send interrupt to a thread?" Watch "Why send interrupt to a thread?" New topic

Why send interrupt to a thread?

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214

I understand the application of the interrupt method.

But WHY would one ever call interrupt on a thread that is waiting on an object's lock?

A "real world" illustration would be much appreciated.



SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Nav Letha

Joined: Oct 22, 2006
Posts: 8
Socket not responding...Reader not responding...
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Actually, many people ignore the why of the matter: Depends on what your target user is.
If the sole thread of execution, running both the user-interface and the program it's self, tries to open a socket or something that the operating system does not manage for the programmer - the mouse and everything else for that program is hung. To the user it appears that the program has failed, and it has from a reliability issue, because the user cannot regain control of the machine if the socket call hangs.
The correct way to do this is make a boolean variable in the class instantiated, then occsionally check that variable from another thread. The reason for this is that the only way to break the socket call is to
the socket call. The only way to interrupt it is from another Thread.

Interrupts should only be used as crisis-management.

Not for routine load-balancing.

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Virag Saksena
Ranch Hand

Joined: Nov 27, 2005
Posts: 71
A good real time example is normally a thread sleeps for n seconds before retrying. Some times a high priority event occurs in some other thread and so the sleeping thread needs to be interrupted.

<a href="" target="_blank" rel="nofollow">The Peak of Performance</a>
I agree. Here's the link:
subject: Why send interrupt to a thread?
It's not a secret anymore!