Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write a deadlock

Shulou Source: shulou.com Published: 2022-06-03 14:20:11 10月16日 Update

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.

Tags: Deadlocks threads content more help yes two interests multiple guys buddies articles core knowledge articles references loops Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Shulou Information Redmi NVidia