• 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

Stopping threads with interrupt()

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a thread running and need to stop it, because of undeploying web app.
What I've done is the like the code below:

but that is not actually work! (test with profiler). When ear application is undeployed there were still some threads that are running, so I did the cancel flag like that

that is working good!
Why interrupt() is not working to stop the thread?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InterruptedException clears 'interrupted' flag,
and your loop is infinite because isInterrupted method always return false.
 
Ranch Hand
Posts: 135
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For sake of safety, you'll want to make sure isCancelled is volatile, otherwise writing it from one thread may not be visible in another thread, especially in the server VM (it'll probably work with the client VM). You could also use synchronized blocks to read/write, or AtomicBoolean, or any other thread-safe object.
 
Ranch Hand
Posts: 176
Mac Chrome Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ireneusz Kordal wrote:InterruptedException clears 'interrupted' flag,
and your loop is infinite because isInterrupted method always return false.


Indeed,

If you wish to stop threads using interrupt() then something like this should do the trick:
reply
    Bookmark Topic Watch Topic
  • New Topic