Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Question?

 
Waez Ali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You interrupt a Thread by calling interrupt() on it. That's about it.
 
Waez Ali
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads forum...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic