Thread stack traces with java 6 and jstack

 

Java 6 on Windows now supports the <jstack> utility as a mean to print stack traces (instead of relying solely on CTRL+BREAK).

Quick demo follows.


1) Write a program which is (pretty much) guaranteed to deadlock


public class Deadlock {

	public static void main (String args[]){

		final Object objA = new Object();

		final Object objB = new Object();

		new Thread() {
 		public void run (){
 			while (1==1) {
 				System.out.println ("Thread A synchronizing on objA");
 				synchronized (objA){
 					System.out.println ("Thread A synchronized on objA");
 					System.out.println ("Thread A synchronizing on objB");
 					synchronized (objB){
 						System.out.println ("Thread A synchronized on objB");
 					}
 				}
 			}
 		}
 	}.start();

		new Thread() {
 		public void run (){
 			while (1==1) {
 				System.out.println ("Thread B synchronizing on objB");
 				synchronized (objB){
 					System.out.println ("Thread B synchronized on objB");
 					System.out.println ("Thread B synchronizing on objA");
 					synchronized (objA){
 						System.out.println ("Thread B synchronized on objA");
 					}
 				}
 			}
 		}
 	}.start();

	}

}


2) Run the Deadock program

deadlock

 

 


3) Get the deadlocked process id

jps

 


4) Run jstack -> 1 deadlock detected.

 

jstack

 

 

Advertisements

5 thoughts on “Thread stack traces with java 6 and jstack

  1. Pingback: Avoiding deadlocks with Reentrantlock « Edgblog

  2. Hindagringe

    Guy .. Excellent .. Wonderful .. I’ll bookmark your website and take the feeds alsoI’m happy to seek out numerous helpful info right here within the publish, we’d like develop more strategies on this regard, thanks for sharing. . . . . .

    Reply
  3. Openuksa

    Очень скоро вы заметите, что тепло родных рук и тактильный контакт снимают ваши волнения лучше любого успокоительного средства. А через пару месяцев непременно одолеет повышенная сексуальность, которая не только вернет былую уверенность, но и откроет новые грани прежних отношений.

    Reply

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