permaculture playing cards*
The moose likes Threads and Synchronization and the fly likes  Thread Question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark " Thread Question?" Watch " Thread Question?" New topic
Author

Thread Question?

Waez Ali
Greenhorn

Joined: Jan 10, 2005
Posts: 22
Hi,
when a thread (in Non-Runnable state) gets interrupted by some other thread ,throws InterruptedException when transited to the Runnable state.
My question is
How one thread can be interrupted by some other thread.
How does it happen practically,I mean can we do it,it happens accedently?

Please Explain me

Thanks...
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You interrupt a Thread by calling interrupt() on it. That's about it.


[Jess in Action][AskingGoodQuestions]
Waez Ali
Greenhorn

Joined: Jan 10, 2005
Posts: 22
Hi Ernest,

Can you expalin it with some example.
When do we need to interrupt a thread when we already know that it will
throw exception if we do so

Thanks.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Moving to Threads forum...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It's just a way of sending a signal to a THread, to say "stop what you're doing." There won't necessarily be an Exception -- only if the THread was in the middle of a call to wait(), sleep(), or certain other methods. Otherwise, the interrupt() call will just set a flag that the Thread's runnable can check with isInterrupted().

So let's say you're overridden run() in a THread subclass; it might look like



Now any other thread can call interrupt() to stop this Thread.
Waez Ali
Greenhorn

Joined: Jan 10, 2005
Posts: 22
Thanks
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Ernest, is that a common way to terminate a thread? I have all these while(running) loops with a method to set running to false. Would calling interrupt() and testing isInterrupted() a better choice?


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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

It's very similar to that, but it's both more powerful (because it forces wait() and join() and sleep() to return immediately) and more dangerous (because if it does any of the above, it throws an InterruptedException and can thereby lob a spanner in the works of your logic.) I think it's less commonly used than it should be. I think it's not more widely used just because it's not widely understood.

I just grabbed my copy of Oaks and Wong's "Java Threads, 3rd Edition" (thanks, JavaRanch!) to see what they had to say. There's one page in Chapter 2 that says, in brief, that indeed, the interrupt flag can be used like a custom isRunning flag. They don't say that they think it's good or bad, just that it's an option.
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Originally posted by Ernest Friedman-Hill:
It's just a way of sending a signal to a THread, to say "stop what you're doing."


I think about it in a different way. In my mind, the interrupt() call is the way to awake a Thread if it.




Now any other thread can call interrupt() to stop this Thread.


But in the case you are not using sleep() and wait() (I suppose, this is the main goal) and you cannot _help_ to Threads Planning.

I don't think that InterruptedException can throw a spanner in the works.

This code does the same things that above code does (But it's not very complicated):


But now you can use sleep() or wait() to release processor.

Am I right?


SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread Question?
 
Similar Threads
Thread
InterruptedException
Q on thread at Jxam
Code confusion
Thread Question