美文网首页
Android开发手写一个简单的死锁

Android开发手写一个简单的死锁

作者: 你的益达233 | 来源:发表于2021-01-30 10:10 被阅读0次
    public class TestDeadLock {
    
    public static void main(String[] args) {
    
        Object lock1 = new Object();
        Object lock2 = new Object();
    
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized(lock1) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock2) {
                        System.out.println(Thread.currentThread().getName());
                    }
                }
    
            }
        }).start();
    
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock2) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    synchronized (lock1) {
                        System.out.println(Thread.currentThread().getName());
                    }
                }
            }
        }).start();
    
        System.out.println("主线程结束...");
    }
    }
    

    相关文章

      网友评论

          本文标题:Android开发手写一个简单的死锁

          本文链接:https://www.haomeiwen.com/subject/dfmhtltx.html