It's got the potential for deadlock - but both locks are acquired so quickly together that one thread can get both before the other has the chance to acquire its first one.
It's got the potential for deadlock - but both locks are acquired so quickly together that one thread can get both before the other has the chance to acquire its first one. Put another Thread. Sleep(500); call between the two synchronized statements and it does deadlock: both threads will enter "their" outer lock, sleep, then when they wake up they'll both find that their "inner" lock is already acquired.
This is due to the fact that you synchronized statements are anti-symetrical : for one thread, the outer synchronized lock is the inner one for the other thread and the other way around.
The sleep can introduce non-determinism. You can make it deterministic; see my answer below. – Toby Aug 26 '11 at 10:05 @Toby: Yes, you can make it deterministic - but sleep is a good way of showing how the deadlock can occur, as you can always imagine a program without sleeps executing similarly to one which does use sleeps, simply by virtue of the thread not being scheduled.In other words, a program which fails when you insert sleeps is inherently flawed because you could see the same "accidentally" in real life.
The same isn't true with inserting notifications. – Jon Skeet Aug 26 '11 at 10:11.
It's possible that one of the threads will enter both sychronized sections, blocking the other thread entirely until it's finished.
You need to simulate 'unfortunate timing'. Try adding sleep between two locks: synchronized(fromAccount){ Thread. Sleep(2000); synchronized(toAccount){.
Sleeps as suggested by Jon above can introduce non-determinism, you could make it deterministic using some coordinator like a latch instead. To clarify though, I'm thinking of it as a testing problem: how to prove a deadlock every time and that may not be what you're looking for. See this code for an example and a blog post describing it a little.
The reason of deadlock is that thread A is wait for Thread B to release some resource before A continue; the same to thread B, it won't continue until thread A releases some resource. In other words, A and B wait forever to each other. In the code snippet, synchronized can block other threads for while because only one thread can execute the block at the moment.Thread.sleep() suspend the thread for 500 millisecond, then continue.
The wait forever mutually condition doesn't satisfy, that why it is not deadlock. Following snippet is a good example to illustrate deadlock public class threadTest{ public class thread1 implements Runnable{ private Thread _th2; private int _foo; public thread1(Thread th2){}; public void run(){ for(int I = 0; iFoo; System.out. Print(" final result " + _foo); } } } public class thread2 implements Runnable{ private final thread1 _th1; private int _foo; public thread2(thread1 th1){}; public void Run(){ synchronized(_th1){_th1.wait()}; synchronized(this){ _foo += th1.
_foo(); this.notify(); } } } } } //just ignore the way to access private variable in the class Because there is no mechanism assuring the execution order of two threads, it is very possible thread 2 won't receive the notification from thread1 since it starts lately, thus it waits for the notification before continue execution. Same to thread1, it cant do next execution until it receives notification from thread2. Both of them wait for each other forever, typical deadlock.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.