When synchronized is used too much, a deadlock may occur. What is the deadlock. Let's take a look at the following code to implement deadlocks:
// Deadlock implementation <br/> Class A <br/> {<br/> Public void get () {<br/> system. out. println ("A said: I started, B, give me your resources"); <br/>}< br/> Public void say () {<br/> system. out. println ("A get resources"); <br/>}< br/> Class B <br/>{< br/> Public void get () {<br/> system. out. println ("B said: I started, A, give me your resources"); <br/>}< br/> Public void say () {<br/> system. out. println ("B gets resources "); <br/>}< br/> class mythread implements runnable <br/>{< br/> Public static A = new (); <br/> Public static B = new B (); <br/> Public Boolean flag = false; <br/> Public void run () {<br/> If (FLAG) {<br/> synchronized (a) {<br/>. get (); <br/> try {<br/> thread. sleep (500); <br/>}catch (interruptedexception e) {}</P> <p> synchronized (B) {// This Synchronous Code block is in another Synchronous Code block <br/>. say (); <br/>}</P> <p >}else {<br/> synchronized (B) {<br/> B. get (); <br/> try {<br/> thread. sleep (500); <br/>}catch (interruptedexception e) {}</P> <p> synchronized () {// This Synchronous Code block is in another Synchronous Code block </P> <p> B. say (); <br/>}</P> <p >}< br/> public class demo24 <br/> {<br/> Public static void main (string ARGs []) {<br/> mythread MT1 = new mythread (); <br/> mythread MT2 = new mythread (); <br/> mt1.flag = true; <br/> mt2.flag = false; <br/> thread Th1 = new thread (MT1); <br/> thread Th1 = new thread (MT2 ); <br/> th1.start (); <br/> th2.start (); <br/>}< br/>}
The above code is deadlocked due to synchronization of synchronized. The deadlock occurs when two or more threads wait for the other thread to complete simultaneously, and the program cannot continue to execute. Before interpreting the code, you must first understand what synchronized is. Synchronized defines synchronization, so what is synchronized and what is synchronized?
First, we need to know what a lock is. In Java, each object has an internal lock. If synchronized is declared as a method or code block, the object lock protects the entire method or code block, to call this method or execute this code block, you must obtain the Lock of this object. In addition, only one thread object can be used to execute the protected code at any time.
In the above Code, after the thread TH1 is started, it acquires the Lock of A. When its sleep is complete, it will apply for the lock of B. At this time, his a lock does not give up.
After thread 2nd is started, he gets the Lock of B. When its sleep is complete, he will apply for the lock of A. At this time, his B lock does not give up.
Both sides hold their own locks and apply for the lock from the other side at the same time. Therefore, a deadlock occurs.
For synchronization, the thread and object are synchronized. The thread and object are bound to obtain the object lock.
NOTE: With the code above, we can find that the necessary condition for a deadlock is not to discard the existing lock, but to apply for a new lock at the same time. Therefore, to achieve deadlocks, synchronized will be nested.
In this way, more than two locks can be operated simultaneously, resulting in a deadlock.