oli mueller wrote:I wrote my test as a junit test, and apparently junit terminates all threads in the end...so, actually i didnt have a deadlock - I just didnt realise that all threads were just killed..;).
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Roel De Nijs wrote:During break I created a small program which simulates your approach. But it doesn't deadlock (and I tried with several sleep times).
(see code from previous post)
A possible output:
t1 in lock
t1 out lock
t2 in lock
t2 has to wait
t1 in unlock
t1 out unlock
t2 out lock
t2 in unlock
t2 out unlock
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
oli mueller wrote:Hi,
I basically did what roel described - just within a junit-test
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Sean Keane wrote:I ran your sample program and I got the same output. However something is confusing for me, maybe you can explain?
Roel De Nijs wrote:
Sean Keane wrote:I ran your sample program and I got the same output. However something is confusing for me, maybe you can explain?
You are simply missing one of the most important things in java concurrency Reading the javadoc of Object's wait method will explain a lot.
Sean Keane wrote:
At this point, I assume t2 has the lock, so no other thread can enter any synchronized method on MyObject until t2 leaves the lock method and releases the lock it holds. But then I see this in the output:
t2 has to wait
t1 in unlock
Which obviously goes against what I was thinking. As t1 has gone into the unlock method whilst t2 is still in the lock method.
SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|