• 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

Thread Question?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Threads forum...
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic