Edgblog

February 13, 2008

Thread stack traces with java 6 and jstack

Filed under: java — edgblog @ 4:12 pm

 

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

 

 

4 Comments »

  1. [...] deadlocks with Reentrantlock Filed under: java — edgblog @ 12:17 pm This post showed how multiple threads synchronizing on two resources in different order usually leads to a [...]

    Pingback by Avoiding deadlocks with Reentrantlock « Edgblog — February 16, 2008 @ 12:17 pm | Reply

  2. Somehow i missed the point. Probably lost in translation :) Anyway … nice blog to visit.

    cheers, Notoriety.

    Comment by Notoriety — June 19, 2008 @ 7:34 am | Reply

  3. Wow enjoyed reading your article. I added your rss to my blogreader!!

    Comment by AlielfVoive — December 11, 2009 @ 8:34 pm | Reply

  4. 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. . . . . .

    Comment by Hindagringe — September 22, 2011 @ 10:48 am | Reply


RSS feed for comments on this post. TrackBack URI

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 )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.