Deadlock
Screw is a very useful tool with many applications because it is very simple to use and easy to understand. But at the same time, it will also bring some trouble, that is, it may cause deadlock. Once a deadlock occurs, the system functions will be unavailable. Let's first look at a piece of code, which will cause deadlocks, causing thread thread_1 and thread thread_2 to wait for each other to release the lock.
package thread;public class DeadLockDemo {private static String A = "A";private static String B = "B";public static void main(String args[]) {new DeadLockDemo().deadLock();}private void deadLock() {// thread thread_1 Thread thread_1 = new Thread(new Runnable() {@Override public void run() {synchronized (A) {System.err.println("--thread_1 lock A----");synchronized (B) {System.err.println("--thread_1 lock B----");}}}});// Thread thread_2 Thread thread_2 = new Thread(new Runnable() {@Override public void run() {synchronized (B) {System.out.println("--thread_2 lock B-----");synchronized (A) {System.out.println("--thread_2 lock A----");}}}});thread_1.start();thread_2.start();}}}This code is just a demonstration of deadlock scenarios, and in reality you may not write such code. But in some more complex scenarios, you may encounter such problems, such as thread_1 has not released the cable because some abnormal situations (dead loop). Or thread_1 gets a database cable, and an exception is thrown when releasing the lock, but it is not released.
Once a deadlock occurs, the business is perceptible because the service cannot be provided. Then you can only check which thread has the problem through the dump thread. The following thread information tells us that lines 35 and 21 of the DeadLockDemo class caused the deadlock.
"Thread-1" prio=6 tid=0x000000000cb13800 nid=0x19ac waiting for monitor entry [0 x000000000d67f000] java.lang.Thread.State: BLOCKED (on object monitor) at thread.DeadLockDemo$2.run(DeadLockDemo.java:35) - waiting to lock <0x00000007d5a9be88> (a java.lang.String) - locked <0x00000007d5a9beb8> (a java.lang.String) at java.lang.Thread.run(Unknown Source) "Thread-0" prio=6 tid=0x0000000000cb0e800 nid=0x6bc waiting for monitor entry [0x 000000000d48f000] java.lang.Thread.State: BLOCKED (on object monitor) at thread.DeadLockDemo$1.run(DeadLockDemo.java:21) - waiting to lock <0x00000007d5a9beb8> (a java.lang.String) - locked <0x00000007d5a9be88> (a java.lang.String) at java.lang.Thread.run(Unknown Source)
Several common ways to avoid deadlocks.
Avoid acquiring multiple locks at the same time as a thread.
Avoid one thread occupying multiple resources at the same time in the rope, and try to ensure that each rope only consumes one resource.
Try using timed searching, using lock.tryLock(timeout) instead of using the internal searching mechanism.
For database cables, locking and unlocking must be in a database connection, otherwise unlocking will fail.
reference:
//www.VeVB.COM/article/131946.htm
//www.VeVB.COM/article/131943.htm
Summarize
The above is the full code analysis of the common methods of Java to avoid deadlocks in this article. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!