[Deploying Sakai] killing threads

Charles Hedrick hedrick at rutgers.edu
Wed Oct 28 12:56:20 PDT 2009


I apologize if this is obvious to people, but I just figured out how  
to kill runaway threads. We've run into a couple of bugs that create  
infinite loops. I'd rather not have to restart a JVM because one  
thread is looping. I found that the following works:

1) use "prstat -L" (Solaris, surely there's a Linux equivalent) to  
find which thread is looping. convert the thread ID from decimal to hex
2) do "kill -QUIT" on the jvm to get a stack trace
3) Look for the thread in the stack trace. It will show as nid=  and  
the hex thread ID. Find the name of the thread.
4) Use the following JSP to get a list of threads. Look for the name  
of the thread. Write down its ID, from the first column. This is the  
Java thread ID, as opposed to the OS thread ID used so far

<%

         int numThreads = Thread.activeCount();
         Thread[] threads = new Thread[numThreads];
         numThreads = Thread.enumerate(threads);

         // Enumerate each thread in oup'
         out.println(numThreads + "<br>");
         for (int i=0; i<numThreads; i++) {
             // Get thread
             Thread thread = threads[i];
             out.println(thread.getId() + " " + thread.getName() +  
"<br>");
         }

%>


5) Add a stop, i.e.

         for (int i=0; i<numThreads; i++) {
             // Get thread
             Thread thread = threads[i];
             out.println(thread.getId() + " " + thread.getName() +  
"<br>");
             if (thread.getId() == NNN)
                       thread.stop();
         }


where NNN is the Java thread ID

I just did this on three different JVMs, suffering from two different  
kinds of loop. It either killed or stopped all the threads.

At some point I'll write a real application to automate this. This may  
depend upon thread management. Our Java uses one OS thread per Java  
thread. I believe there are modes in which it shares OS threads. I  
have no idea how to solve this problem in that case.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2421 bytes
Desc: not available
Url : http://collab.sakaiproject.org/pipermail/production/attachments/20091028/29c6e5b8/attachment.bin 


More information about the production mailing list