死锁演示

来源:互联网 发布:ai做淘宝照片尺寸 编辑:程序博客网 时间:2024/06/05 18:50
package com.demo;/* * 死锁演示 */public class DeadLockDemo {public static void main(String[] args) {Test a = new Test(true);Test b = new Test(false);Thread t1 = new Thread(a);Thread t2 = new Thread(b);t1.start();t2.start();}}class Test implements Runnable {private boolean flag;public Test(boolean flag) {this.flag = flag;}public void run() {if (flag) {synchronized (MyLock.locka) {System.out.println(Thread.currentThread().getName()+ ":if -----locka----");synchronized (MyLock.lockb) {System.out.println(Thread.currentThread().getName()+ ":if -----lockb----");}}} else {synchronized (MyLock.lockb) {System.out.println(Thread.currentThread().getName()+ ":else -----lockb----");synchronized (MyLock.locka) {System.out.println(Thread.currentThread().getName()+ ":else -----locka----");}}}}}class MyLock {public static final Object locka = new Object();public static final Object lockb = new Object();}

原创粉丝点击