Java Tutorial, Concurrency trail

I

Ian

Hi all,

I have a question regarding the Java tutorial, concurrency trail, which
contains an example of deadlock between two bowing friends.

http://download.oracle.com/javase/tutorial/essential/concurrency/deadlock.html

There is a forum question on this subject, but I don't believe the OPs
question is answered properly:

http://forums.oracle.com/forums/thread.jspa?threadID=1140695&tstart=120.

I have observed the same results as the OP, but I can't reconcile what
happens with any of the tutorial material (the code is pretty much
posted into the tutorial without detailed comment). Is anyone here
familiar with that code?

As far as I can tell, alphonse's thread (by this I mean the first one)
grabs the default monitor for the synchronized bow method, and gaston's
thread (ie the second one) should be blocked, so how does gaston's
thread acquire the lock at all?

I believe that alphonse should therefore be able to bow back, and
release the lock for gaston, but there seems to be a timing window that
allow both to acquire the same lock simultaneously - impossible?

BTW I have altered the deadlock code so that the two methods use
synchronized blocks against different monitors, and the code works fine,
so I am certain that both threads must be holding the same lock. Also,
debugging causes the original program to run as I would expect.

Can anyone enlighten me, I'm sure I'm missing out on something
fundamental here . . .

Hope this is all understandable, Ian.

PS no I don't care how it would be done in c++ ;)
 
J

Joshua Cranmer

As far as I can tell, alphonse's thread (by this I mean the first one)
grabs the default monitor for the synchronized bow method, and gaston's
thread (ie the second one) should be blocked, so how does gaston's
thread acquire the lock at all?

A method:
synchronized void foo() { doSomething(); }

is equivalent to [1]:
void foo() { synchronized(this) { doSomething(); } }, in terms of the
lock acquired.

Alphonse's bow method acquires his monitor to bow, and then attempts to
acquire Gaston's lock to have him bow back--without releasing his own lock.

Gaston's bow method acquires *his* monitor to bow and then attempts to
acquire Alphonse's bow to bow back.

[1] If the method is static, it is Foo.class, where Foo is the class the
method is defined in.
 
I

Ian

Hi Peter,

Thanks to you, Patricia and Joshua (and anyone else who replies) for
your answers, I think there is a good lesson to be learned here -
digesting . . .

Ian.
 
I

Ian

[1] If the method is static, it is Foo.class, where Foo is the class the
method is defined in.

BTW Your footnote actually contained my mistake - I was imagining the
lock(s) as a single static one instead of two instances each with their
own - Duh!

Thanks again to all who answered,

Ian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top