Java Multithreading – Thread Tracer
This program can be integrated with your any java program to trace various states of a thread. It can help you to understand the flow of a thread. But remember, Tracer runs a thread to trace states of a thread. Tracer will not able to print all states of a thread. since there might be a situation when Tracer thread get served to CPU very late or with big interval and another thread changes its 3-4 states in between. I generally run Tracer 2-3 times for proper understanding.
I took its help to find out deadlock in my one of the program over nested monitor.
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
/*
* (C) Copyright 2012-2013, by Amit Gupta (http://article-stack.com/).
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA. OR see <http://www.gnu.org/licenses/>.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------------
* Tracer.java
* ---------------------
* *
*/
public class Tracer {
private ArrayList<WrapperThread> threadList;
Tracer(){
threadList = new ArrayList<WrapperThread>();
}
public void add(Thread t){
threadList.add(new WrapperThread(t));
}
static{
new Thread(new Runnable(){
@Override
public void run() {
System.out.println("Monitoring Deadlocks");
while(Thread.activeCount() >= 2)
deadLockMonitor();
}
},"DeadLock Monitor").start();
}
private static void deadLockMonitor(){
ThreadMXBean mx = ManagementFactory.getThreadMXBean();
long[] DevilThreads = mx.findDeadlockedThreads();
if(DevilThreads != null && DevilThreads.length > 0){
System.out.println(currentTime() + "Deadlock detected");
for(int i=0;i<DevilThreads.length; i++){
System.out.println("Thread id :" + DevilThreads[i]);
}
System.out.println("Exiting from system");
System.exit(0);
}
}
public void trace() {
new Thread(new Runnable(){
@Override
public void run() {
Iterator<WrapperThread> itr;
while(Thread.activeCount() >= 2){
itr = threadList.iterator();
while(itr.hasNext()){
WrapperThread wt = itr.next();
if(wt.isStateChanged()){
System.out.print(currentTime() + " :: " + wt.originalThread.getName()+":"+wt.currentState + "\n");
}
}
}
}
},"Tracer").start();
}
public static void traceImmediate(final Thread t) {
new Thread(new Runnable(){
@Override
public void run() {
WrapperThread wt = new WrapperThread(t);
while(Thread.activeCount() >= 2){
if(wt.isStateChanged()){
System.out.print(currentTime() + " :: " + wt.originalThread.getName()+":"+wt.currentState + "\n");
}
}
}
},"Immediate Tracer").start();
}
public static String currentTime(){
return (new Timestamp(System.currentTimeMillis())).toString();
}
}
How to use it in your program
Tracer t = new Tracer(); t.add(A); t.add(B); t.trace();
Or
Tracer.traceImmediate(A); Tracer.traceImmediate(B);
Happy coding
53
views
views


No Comments