kioria (1) [Avatar] Offline
#1
Dear author,
I am wondering about listing com.java7developer.chapter4.DeadlockMicroBlogNode downloaded from your site.
When running it in IDE, I have got outputs like this one
localhost:8888: recvd: 1 ; backup: localhost:8988
localhost:8988: recvd: 2 ; backup: localhost:8888

It is explained later "The lock is aquired at the start of the synchronized method ....and released at the end of the synchrnized method", page 93.
The listing for that synchronized method is below
@Override
public synchronized void propagateUpdate(Update upd_,
SimpleMicroBlogNode backup_) {
System.out.println(this.hashCode());
System.out.println(ident + ": recvd: " + upd_.getUpdateText()
+ " ; backup: " + backup_.getIdent());
backup_.confirmUpdate(this, upd_);
System.out.println(Thread.currentThread().getName()+" exits");
}


(I have added additional sysout to know when thread exits synchronized method thus releasing its lock on it)
From the output above you can see that line "System.out.println(Thread.currentThread().getName()+" exits");" is never printed by any thread.

So the question is: how could it happen, that second thread starts executing synchronized method at a time when the first thread didn't finish it ( didn't print System.out.println(Thread.currentThread().getName()+" exits")smilie ?

Or invocing backup_.confirmUpdate(this, upd_) on the other thread instance from the current one provokes some unpredictable code execution order?

Please, guide me through that code as i am missing something.

Java stack trace is below
===================================================
"secondThread":
at com.java7developer.chapter4.DeadlockMicroBlogNode.confirmUpdate(DeadlockMicroBlogNode.java:35)
- waiting to lock <0x22fc10c8> (a com.java7developer.chapter4.DeadlockMicroBlogNode)
at com.java7developer.chapter4.DeadlockMicroBlogNode.propagateUpdate(DeadlockMicroBlogNode.java:2smilie
- locked <0x22fc10d8> (a com.java7developer.chapter4.DeadlockMicroBlogNode)
at com.java7developer.chapter4.DeadlockMicroBlogNode$2.run(DeadlockMicroBlogNode.java:64)
at java.lang.Thread.run(Thread.java:722)
"firstThread":
at com.java7developer.chapter4.DeadlockMicroBlogNode.confirmUpdate(DeadlockMicroBlogNode.java:35)
- waiting to lock <0x22fc10d8> (a com.java7developer.chapter4.DeadlockMicroBlogNode)
at com.java7developer.chapter4.DeadlockMicroBlogNode.propagateUpdate(DeadlockMicroBlogNode.java:2smilie
- locked <0x22fc10c8> (a com.java7developer.chapter4.DeadlockMicroBlogNode)
at com.java7developer.chapter4.DeadlockMicroBlogNode$1.run(DeadlockMicroBlogNode.java:53)
at java.lang.Thread.run(Thread.java:722)

Found 1 deadlock



Regards, kioria

Message was edited by:
kioria