In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "how to solve the deadlock problem in Java thread technology". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope this article "how to solve the deadlock problem in Java thread technology" can help you solve the problem.
We know that using the synchronized keyword can effectively solve the problem of thread synchronization, but it can also cause problems if the synchronized keyword is not used properly, which is what we call a deadlock. A deadlock is a situation in which multiple threads are blocked at the same time and one or all of them are waiting for a resource to be released. Because the thread is blocked indefinitely, it is impossible for the program to terminate normally.
Let's write an example of a deadlock, and then analyze the cause of the deadlock:
Public class DeadLock {public static void main (String [] args) {Business business = new Business1 (); / / start a thread to execute the functionA method new Thread (new Runnable () {@ Override public void run () {while (true) {business.functionA ()) in the Business class }) .start (); / / start another thread to execute the functionB method new Thread (new Runnable () {@ Override public void run () {while (true) {business.functionB ()) in the Business class }) .start ();}} class Business {/ / define two locks, two methods / / define two locks public static final Object lock_a = new Object (); public static final Object lock_b = new Object () Public void functionA () {synchronized (lock_a) {System.out.println ("- ThreadA---lock_a---"); synchronized (lock_b) {System.out.println ("- ThreadA---lock_b---") } public void functionB () {synchronized (lock_b) {System.out.println ("- ThreadB---lock_b---"); synchronized (lock_a) {System.out.println ("- ThreadB---lock_a---");}
The structure of the program is very clear and there is no difficulty. First, take a look at the execution result of the program:
-ThreadA---locka
-ThreadA---lockb
-ThreadA---locka
-ThreadA---lockb
-ThreadA---locka
-ThreadA---lockb
-ThreadA---locka
-ThreadB---lockb
Judging from the execution results, thread An is running, and when thread B runs, it dies with a snap. Let's analyze why: as you can see from the above code, a class Business is defined, in which two locks and two methods are maintained, each of which is a synchronized chain and uses a different lock. OK, now open two threads An and B in the main method and execute the two methods in the Business class, respectively. Thread A gives priority to execution and runs very well, and when thread B starts to execute, the problem arises. From the last two lines of the execution result, thread An enters the first synchronized in the functionA method and gets the lock_a lock. Thread B enters the first synchronized in the functionB and gets the lock_b lock, and both locks have not been released. Then there is the key: when thread An enters the second synchronized, it finds that lock_b is being occupied by B, so there is no way, so it has to be blocked and wait. similarly, when thread B enters the second synchronized, it finds that lock_a is being occupied by A, and it has no choice but to be blocked. OK, the two just wait for each other, you don't let go, I won't let go. Dead.
The above program is very helpful for understanding deadlocks, because the structure is very good, but I feel that this is not good enough, because the two threads implement two different Runnable interfaces, but only call two methods of the same class, because I put the methods to be synchronized into the same class. Next, I will change the program, put the code to be synchronized into a Runnable, and let it hang up as soon as it runs.
Public class DeadLock {public static void main (String [] args) {/ / start two threads and throw two custom Runnable into new Thread (new MyRunnable (true)). Start ();; new Thread (new MyRunnable (false)). Start ();}} class MyRunnable implements Runnable {private boolean flag / / used to determine and execute different synchronous code blocks MyRunnable (boolean flag) {/ / constructor this.flag = flag } @ Override public void run () {if (flag) {while (true) {synchronized (MyLock.lock_a) {System.out.println ("--threadA---lock_a--") Synchronized (MyLock.lock_b) {System.out.println ("--threadA---lock_b--") } else {while (true) {synchronized (MyLock.lock_b) {System.out.println ("--threadB---lock_a--") Synchronized (MyLock.lock_a) {System.out.println ("--threadB---lock_b--") } class MyLock / / two locks are defined in one class so that both threads use both locks {public static final Object lock_a = new Object (); public static final Object lock_b = new Object ();}
This deadlock is very powerful. As soon as it runs, it will hang up with a snap. Take a look at the running results:
-- threadA---locka--
-- threadB---lockb--
This is the end of the content on "how to solve the deadlock problem in Java threading technology". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.