Home > Java, Multithreading > Thread Tracer

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;

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(() -> {
                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(() -> {
                while (Thread.activeCount() >= 2) {
                    if (isStateChanged()) System.out.println (currentTime() + " :: "+threadList);
                }
        }, "Tracer").start();
    }
 
    private boolean isStateChanged(){
    	for (WrapperThread wT : threadList) {
    		if(wT.isStateChanged()) return true;
	}
    	return false;
    }
 
    public static String currentTime() {
        return (new Timestamp(System.currentTimeMillis())).toString();
    }
}

Wrapper Thread

import java.lang.Thread.State;

public class WrapperThread{
	private String name;
	protected Thread originalThread;
	private State lastState;

	public WrapperThread(Thread t) {
		originalThread = t;
		name = t.getName();
		lastState = originalThread.getState();
		System.out.println(Tracer.currentTime() + " :: " + this);
	}

	public boolean isStateChanged() {
		State state = originalThread.getState();
		if(lastState != state){
			lastState = state;
			return true;
		}
		return false;
	}

	public State currentState() {
		return originalThread.getState();
	}
	
	@Override
	public String toString() {
		return name+":"+currentState();
	}
}

How to use it in your program

Tracer t = new Tracer();
t.add(A);
t.add(B);
t.trace();

Happy coding 🙂

Advertisements
Categories: Java, Multithreading
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: