Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interrupt Java Worker Thread  RSS feed

 
Madhu Kaparapu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Rohan Dhapodkar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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()


 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!