synchronized keyword was not supposed to be reentrant ?

Synchronized block in the below code seems to be working properly as a reentrant code. I've written this to check out the reentrancy by causing a deadlock but it worked without deadlock./* This code might cause to a deadlock */

publicclass NotReentrant{

publicstaticvoid main(String[] args){

final LoggingWidget widget =new LoggingWidget();

Thread t1 =new Thread(new Runnable(){

publicvoid run(){ widget.execute();}

});

t1.start();

try{ t1.join();}catch (Exception e){}

System.out.println("You might not going to see this: There is a deadlock !");

}

}

/* dummy classes that show the proof */

class Widget{

publicsynchronizedvoid execute(){

System.out.println(toString() +" (base).execute: synchronized");

synchronized (this){

System.out.println(toString() +" (base).execute: synchronized (this)");

try{ Thread.sleep(500);}catch (Exception e){}

System.out.println(toString() +" (base).execute");

// .... code

}

}

}

class LoggingWidgetextends Widget{

publicsynchronizedvoid execute(){

System.out.println(toString() +".execute: synchronized");

super.execute();

System.out.println(toString() +".execute");

}

}

[3104 byte] By [Inanc.Gumusa] at [2007-11-26 17:16:30]
# 1

What deadlock were you expecting?

There was only 1 new object created: a LoggingWidget object,

and only 1 new thread created.

All the synchronization occurs in the new thread,

and they were all done on the same LoggingWidget object,

so they were all allowed to do it immediately.

Suntra_Vineeta at 2007-7-8 23:44:34 > top of Java-index,Core,Core APIs...
# 2

I don't know what your code was trying to "prove" to you but I suggest looking at the FAQ and reading some of the referenced introductions to Java concurrency to understand how Java monitors (aka synchronized blocks/methods) work. Java monitors are reentrant and precisely for the type of code you used: super calls to synchronized methods, from within synchronized methods must not deadlock.

davidholmesa at 2007-7-8 23:44:34 > top of Java-index,Core,Core APIs...
# 3

I perfectly understand how Java monitors work and I also perfectly know that Java synchronized ( aka intrinsic locks ) monitor is re-entrant.

The reason behind my previous post was that I am trying to check out a code which is written in Goetz's book: Java Concurreny in Practice. In there, he gives a code example that leads to a deadlock. Despite, I am aware of the fact that it cannot be occurred, I have given it a shot.

Thanks for the comments.

Inanc.Gumusa at 2007-7-8 23:44:34 > top of Java-index,Core,Core APIs...
# 4
Just because code can lead to deadlock doesn't mean you will encounter that deadlock when you run the code. Some deadlocks are easier to encounter than others.Which example for JCiP were you working from?
davidholmesa at 2007-7-8 23:44:34 > top of Java-index,Core,Core APIs...