Java multi-thread deadlock
What is a deadlock: a deadlock occurs when two or more processes compete for resources during execution. If there is no external force, they will not be able to proceed. What are the causes and conditions of deadlocks?
1. Due to insufficient system resources.
2. The process running sequence is not appropriate.
3. improper resource allocation.
Anyone who has learned the operating system knows that there are four conditions for deadlock:
1. mutex condition: A process Excludes resources at a specific time.
2. Request and retention conditions: when a process is blocked by requesting resources, it will not release the obtained resources.
3. Non-deprivation condition: the process has obtained resources, and cannot be forcibly deprived before the end of use.
4. Cyclic waiting condition: a type of cyclic waiting resource relationship is formed between several processes.
Example Analysis
The lock is because multiple threads access Shared resources. Due to improper access sequence, A thread usually locks resource A and wants to lock resource B. In another thread, resource B is locked, and resource A is locked to complete its own operations. Both threads want to get the other's resources rather than release their own resources, causing both threads to be waiting, and cannot be executed.
It is not difficult to analyze the cause of the deadlock to see that it is caused by improper access to shared resources. The following is an example of thread deadlock. I hope to have a better understanding of the multi-thread deadlock problem! If someone needs to write a multi-threaded system, be very careful when operating shared resources to prevent deadlocks!
Code
Class Zhangsan {// defines three types of public void say () {System. out. println ("Zhang San said to Li Si:" If you paint for me, I will give you the book ." ");} Public void get () {System. out. println (" Zhang San got the picture. ") ;}}; Class Lisi {// defines four classes of public void say () {System. out. println ("Li Si said to James," You give me a book, I'll give it to you "");} public void get () {System. out. println ("Lee got the book. ") ;}}; Public class ThreadDeadLock implements Runnable {private static Zhangsan zs = new Zhangsan (); // instantiate a static object. Once it is created, it will exist until the program exits. Private static Lisi ls = new Lisi (); // instantiate the static object private boolean flag = false; // declare the flag to determine the first speaker public void run () {// override run () method if (flag) {synchronized (zs) {// synchronize zs. say (); try {Thread. sleep (500);} catch (InterruptedException e) {e. printStackTrace ();} synchronized (ls) {zs. get () ;}} else {synchronized (ls) {ls. say (); try {Thread. sleep (500);} catch (InterruptedException e) {e. printStackTrace ();} synchronized (zs) {ls. get () ;}}} public static void main (String args []) {ThreadDeadLock t1 = new ThreadDeadLock (); // controls Michael ThreadDeadLock t2 = new ThreadDeadLock (); // control Li Si t1.flag = true; t2.flag = false; Thread thA = new Thread (t1); Thread thB = new Thread (t2); thA. start (); thB. start ();}};
Running effect:
In conclusion, when writing code, try to avoid deadlocks. Through today's learning, we have mastered the problem of multi-thread deadlock.