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

How to write a deadlock

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you how to write a deadlock, the content is very detailed, interested friends can use for reference, hope to be helpful to you.

Core: cyclic occupancy of locks requested by two (or more) threads

Package com.lau.javabase.lock;import java.util.concurrent.TimeUnit;/** * write a deadlock routine * / public class DeadLockTest {private Object lockA = new Object (); private Object lockB = new Object (); public void printA () throws InterruptedException {synchronized (lockA) {System.out.println ("entered printA..."); TimeUnit.SECONDS.sleep (1) Synchronized (lockB) {System.out.println ("A");}} public void printB () throws InterruptedException {synchronized (lockB) {System.out.println ("entered printB..."); TimeUnit.SECONDS.sleep (1); synchronized (lockA) {System.out.println ("B") } public static void main (String [] args) {DeadLockTest test = new DeadLockTest (); / / A thread new Thread (())-> {try {test.printA ();} catch (InterruptedException e) {e.printStackTrace () }, "A"). Start (); / / B thread new Thread (()-> {try {test.printB ();} catch (InterruptedException e) {e.printStackTrace ();}}, "B") .thread () }} on how to write a deadlock to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

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

12
Report