In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the use of deadlocks in Java multithreading, which is very detailed and has certain reference value. Friends who are interested must finish reading it.
The so-called thread deadlock means that two or more threads hold the resources needed by each other. Because of the characteristics of synchronized, one thread holds a resource, or acquires a lock. Before the thread releases the lock, other threads will not get the lock and will wait forever, so this leads to a deadlock.
The following figure (1-1):
The necessary conditions for deadlocks in Java multithreading are as follows:
1. Mutex condition: a resource, or a lock, can only be occupied by one thread, when a thread first acquires the lock
No other thread can acquire the lock until it is released by the thread
two。 Possess and wait: when a thread has acquired a lock and then acquired another lock, it will not release the already
Acquired lock
3. Inalienable condition: no thread can force a lock that is already occupied by another thread
4. Loop wait condition: thread A holds thread B's lock, thread B holds thread A's lock
When the above four conditions are met, there is a deadlock!
Public class DeadLock implements Runnable {
/ / create two lock objects
Private Object lock1=new Object ()
Private Object lock2=new Object ()
@ Override
Public void run () {
While (true) {
Method1 ()
Method2 ()
}
}
Public void method1 () {
Synchronized (lock1) {
System.out.println (Thread.currentThread () .getName () + "get lock1")
Try {
Thread.sleep (1000); / / Thread hibernates, causing CPU to switch
} catch (InterruptedException e) {
E.printStackTrace ()
}
Synchronized (lock2) {
System.out.println (Thread.currentThread () .getName () + "get lock2")
}
}
}
Public void method2 () {
Synchronized (lock2) {
System.out.println (Thread.currentThread () .getName () + "get lock2")
Synchronized (lock1) {
System.out.println (Thread.currentThread () .getName () + "get lock1")
}
}
}
Public static void main (String [] args) {
DeadLock deadLock = new DeadLock ()
New Thread (deadLock) .start ()
New Thread (deadLock) .start ()
}
}
(figure 3-1)
This creates a deadlock. Thread1 holds lock1, wants lock2,Thread2 to hold lock2, wants lock1.
How to avoid deadlocks in Java multithreading:
1. Locking order: threads are locked in the same order.
two。 Lock time limit, the thread is limited to a certain amount of time in the process of acquiring the lock. If you can't get it within a given time, forget it. Don't force yourself.
Oneself. This requires some API from Lock
After the deadlock, the program gets stuck and doesn't respond, but the program is still running, so it needs some help
1. First, use jps-l to show the running virtual machine process and show the virtual machine execution main class (where the main function is located
And the unique ID of the local virtual machine for these processes
Figure (5-1)
two。 Use jstack + id for tracking and troubleshooting
Figure (5-2)
Figure (5-3)
These are all the contents of the article "what is the use of deadlocks in Java multithreading". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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: 279
*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.