• 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

Interrupt Java Worker Thread

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

We have a custom Servlet Engine (Similar to Tomcat) which has configurable numbers of "Worker Threads" to handle HTTP request. The issues here is, this engine hangs if the "Worker Threads" are BLOCKED for long time. So I wanted to interrupt these "Worket Threads".

When I took the thread dump with "jstack <pid>" will give the all the Worker Thread's name, address, tid , nid, and state. So, having these thread info can we build thread object to interrupt blocked threads and any other approach to interrupt BLOCKED threads.

Please suggest your thoughts.

Thanks,
Madhu K.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interrupting a thread doesn't magically solve your problem, the worker thread has to cooperatively deal with the interruption. The worker thread needs to ...

1. check if the interrupt flag is set, once in a while, and prep for clean up and exit from the current task
2. for calls that can be interrupted, catch the InterruptedException, and prep for clean up and exit from the current task

3a. if the OS/JVM supports interrupted IO, catch the InterruptedIOException, and prep for clean up and exit from the current task
3b. if the OS/JVM does not support interrupted IO, then the interrupting thread needs to set some other flag, and close the IO, and.... the worker thread needs to deal the IOException (and distinguish that it is being interrupted by looking at the some other flag), and prep for clean up and exit from the current task


And of course, the "prep for clean up" part is a short way of saying, doing whatever is needed to clean the mess up, since the worker thread is no longer expected to finish what it was doing.

Henry
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
with Java5, you can use Thread.getAllStackTraces(), to get list of all threads and then you can check status of thread using Thread.getState()


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic