In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is the cause of Java thread deadlock". The content of the article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the cause of Java thread deadlock"!
What is a deadlock
Deadlock refers to a blocking phenomenon caused by competition for resources or communication between two or more processes in the process of execution, which will not be able to move forward without external force. At this point, it is said that the system is in a deadlock state or the system has a deadlock, and these processes that are always waiting for each other are called deadlock processes.
Second, the cause of deadlock
1. Mutually exclusive condition: refers to the exclusive use of allocated resources by a process, that is, a resource is occupied by only one process for a period of time. If there are other processes requesting resources at this time, the requestor can only wait until the process in possession of the resource is released.
2. Request and retention conditions: the process has maintained at least one resource, but a new resource request has been made, and the resource has been occupied by other processes. At this time, the requesting process is blocked, but does not let go of other resources that it has acquired.
3. No deprivation condition: refers to the resources that have been acquired by the process, which cannot be deprived until they have been used up, but can only be released by themselves at the end of use.
4. Loop waiting condition: when a deadlock occurs, there must be a ring chain of process-resources, that is, P0 in the process set {P0, P110, P2, Pn} is waiting for a resource occupied by P1; P1 is waiting for the resource occupied by P2,. Pn is waiting for resources that have been occupied by P0.
Deadlock demonstration 1. Synchronizedimport lombok.Data;@Datapublic class Studnet {private String name;} public static void main (String [] args) {Studnet stu1=new Studnet (); stu1.setName ("stu1"); Studnet stu2 = new Studnet (); stu2.setName ("stu2") New Thread (()-> {/ / locked stu1 synchronized (stu1) {System.out.println ("Thread:" + Thread.currentThread () .getName () + ", hold:" + stu1.getName () Try {/ / because thread running is the native method, we increase thread sleep and increase deadlock probability TimeUnit.SECONDS.sleep (1);} catch (InterruptedException e) {e.printStackTrace () } / / Lock stu2 synchronized (stu2) {System.out.println ("Thread:" + Thread.currentThread (). GetName () + ", hold:" + stu2.getName ();}, "T1") .start () New Thread (()-> {/ / locked stu2 synchronized (stu2) {System.out.println ("Thread:" + Thread.currentThread () .getName () + ", hold:" + stu2.getName () Try {/ / because thread running is the native method, we increase thread sleep and increase deadlock probability TimeUnit.SECONDS.sleep (1);} catch (InterruptedException e) {e.printStackTrace () } / / Lock stu1 synchronized (stu1) {System.out.println ("Thread:" + Thread.currentThread (). GetName () + ", hold:" + stu1.getName ());}, "T2") .start ();}
2. Lock public static void main (String [] args) {Lock lock1=new ReentrantLock (); Lock lock2=new ReentrantLock (); new Thread ()-> {/ / locked lock1 lock1.lock (); try {System.out.println ("thread:" + Thread.currentThread (). GetName () + ", hold:" + "lock1") Try {/ / because thread running is the native method, we increase thread sleep and increase deadlock probability TimeUnit.SECONDS.sleep (1);} catch (InterruptedException e) {e.printStackTrace () } / / Lock lock2 lock2.lock (); try {System.out.println ("Thread:" + Thread.currentThread (). GetName () + ", hold:" + "lock2");} finally {/ / release lock2 lock2.unlock () }} finally {/ / release lock1 lock1.unlock ();}}, "T1") .start (); new Thread (()-> {lock2.lock () Try {System.out.println ("thread:" + Thread.currentThread (). GetName () + ", hold:" + "lock2"); try {/ / because thread running is a native method, we increase thread sleep and increase deadlock probability TimeUnit.SECONDS.sleep (1) } catch (InterruptedException e) {e.printStackTrace ();} lock1.lock (); try {System.out.println ("Thread:" + Thread.currentThread (). GetName () + ", hold:" + "lock1") } finally {lock1.unlock ();}} finally {lock2.unlock ();}}, "T2") .start ();}
4. How to view the deadlock 1. Use the jps command to find the pid running the program
Jps
2. Jstack to view stack information
Jstack pid
Found a deadlock.
Key summary, T1, T2 threads, cross-hold locks, waiting for each other's resources.
"T2":-waiting to lock-locked
# T2 wait for lock 0x000000076b6f8428, hold 0x000000076b6f8468
"T1":-waiting to lock-locked
# T1 wait for lock 0x000000076b6f8468, hold 0x000000076b6f8428
Thank you for your reading, the above is the content of "what is the cause of java thread deadlock". After the study of this article, I believe you have a deeper understanding of what is the cause of Java thread deadlock, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.