Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Java deadlock Generation

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Java deadlock generated sample analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

There are many key points for us to pay attention to in Java deadlock generation. In fact, there are many questions that can be answered in the source code. Let's take a look at how to make Java deadlock generation better.

Public class Deadlock extends Object... {private String objID; public Deadlock (String id)... {objID = id;} public synchronized void checkOther (Deadlock other)... {print ("entering checkOther ()"); / / simulate some lengthy process try... {Thread.sleep (2000);} catch (InterruptedException x)... {} print ("in checkOther ()-about to" + "invoke 'other.action ()"); other.action () Print ("leaving checkOther ()");} public synchronized void action ()... {print ("entering action ()"); / / simulate some work here try... {Thread.sleep (500);} catch (InterruptedException x)... {} print ("leaving action ()");} public void print (String msg)... {threadPrint ("objID=" + objID + "-" + msg) } public static void threadPrint (String msg)... {String threadName = Thread.currentThread (). GetName (); System.out.println (threadName + ":" + msg);} public static void main (String [] args). {final Deadlock obj1 = new Deadlock ("obj1"); final Deadlock obj2 = new Deadlock ("obj2"); Runnable runA = new Runnable (). {public void run (). {obj1.checkOther (obj2);}} Thread threadA = new Thread (runA, "threadA"); threadA.start (); try... {Thread.sleep (200);} catch (InterruptedException x)... {} Runnable runB = new Runnable ()... {public void run ()... {obj2.checkOther (obj1);}}; Thread threadB = new Thread (runB, "threadB"); threadB.start (); try... {Thread.sleep (5000) } catch (InterruptedException x)... {} threadPrint ("finished sleeping"); threadPrint ("about to interrupt () threadA"); threadA.interrupt (); try... {Thread.sleep (1000);} catch (InterruptedException x)... {} threadPrint ("about to interrupt () threadB"); threadB.interrupt (); try... {Thread.sleep (1000);} catch (InterruptedException x)... {} threadPrint ("did that break the deadlock?") }}

The above is a detailed introduction to the generation of Java deadlock, I hope you can understand.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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: 253

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report