@@ -0,0 +1,1616 @@
+import java.util.concurrent.locks.LockSupport;%0A%0Apublic class DemoThreadMonitoring %7B%0A%09public static final long nanos = 1000L;%0A%09static SingleElementBlockingQueue q;%0A%09static Thread producer, consumer;%0A%0A%09static volatile boolean done = false;%0A%0A%09static class Producer implements Runnable %7B%0A%09%09public void run() %7B%0A%09%09%09while (!done) q.write(%22hello%22);%0A%09%09%7D%0A%09%7D%0A%0A%09static class Consumer implements Runnable %7B%0A%09%09public void run() %7B%0A%09%09%09while (!done) q.remove();%0A%09%09%7D%0A%09%7D%0A%0A%09static class Monitor implements Runnable %7B%0A%09%09public int numEvents = 0;%0A%09%09public int producerBlocked = 0;%0A%09%09public int consumerBlocked = 0;%0A%09%09@Override%0A%09%09public void run() %7B%0A%09%09%09while (!done) %7B%0A%09%09%09%09numEvents++;%0A%09%09%09%09if ( producer.getState() == Thread.State.BLOCKED ) producerBlocked++;%0A%09%09%09%09if ( consumer.getState() == Thread.State.BLOCKED ) consumerBlocked++;%0A%09%09%09%09LockSupport.parkNanos(nanos);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09public static void main(String%5B%5D args) throws Exception %7B%0A%09%09q = new SingleElementBlockingQueue();%0A%09%09consumer = new Thread(new Consumer());%0A%09%09consumer.start();%0A%09%09producer = new Thread(new Producer());%0A%09%09producer.start();%0A%0A%09%09Monitor monitor = new Monitor();%0A%09%09Thread monitorThread = new Thread(monitor);%0A%09%09monitorThread.start();%0A%0A%09%09Thread.sleep(5000);%0A%09%09done = true;%0A%0A%09%09System.out.printf(%22Producer: %25d/%25d runnable/blocked = %251.4f%25%25%5Cn%22,%0A%09%09%09%09%09%09  monitor.producerBlocked,%0A%09%09%09%09%09%09  monitor.numEvents,%0A%09%09%09%09%09%09  ((float)monitor.producerBlocked*100)/monitor.numEvents);%0A%09%09System.out.printf(%22Consumer: %25d/%25d runnable/blocked = %251.4f%25%25%5Cn%22,%0A%09%09%09%09%09%09  monitor.consumerBlocked,%0A%09%09%09%09%09%09  monitor.numEvents,%0A%09%09%09%09%09%09  ((float)monitor.consumerBlocked*100)/monitor.numEvents);%0A%09%7D%0A%7D%0A

add monitoring thread object