美文网首页
sychronized vs lock interrupt

sychronized vs lock interrupt

作者: 誓言的梦 | 来源:发表于2018-06-10 01:14 被阅读0次
    static Object a = new Object();
    static Object b = new Object();
    
    public static void main(String[] args) throws InterruptedException, IOException {
    
        final CountDownLatch countDownLatch = new CountDownLatch(1);
    
        final Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
    
                System.out.println(Thread.currentThread().getId() + "获取a");
                synchronized (a) {
                    System.out.println(Thread.currentThread().getId() + "进入a");
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getId() + "获取b");
                    synchronized (b) {
                        System.out.println(Thread.currentThread().getId() + "进入b");
    
                    }
                }
            }
        });
        threadA.start();
        final Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
    
                System.out.println(Thread.currentThread().getId() + "获取b");
                synchronized (b) {
                    System.out.println(Thread.currentThread().getId() + "进入b");
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getId() + "获取a");
                     synchronized (a) {
                        System.out.println(Thread.currentThread().getId() + "进入a");
    
                    }
                }
            }
        });
        threadB.start();
    
    
    
        new Thread(new Runnable() {
            @Override
            public void run() {
                    countDownLatch.countDown();
                System.out.println("threada interrupt");
                threadA.interrupt();
                System.out.println("threadb interrupt");
                threadB.interrupt();
                System.out.println("获取aaaaaa 前");
                synchronized (a) {
                    System.out.println("获取aaaaaa");
                }
            }
        }).start();
    
    
        TimeUnit.SECONDS.sleep(10);
    }
    

    sychronized 不响应中断的证明


    image.png

    reentrantLock 响应锁中断

    public static void main(String[] args) {
    final ReentrantLock reentrantLockA = new ReentrantLock();
    final ReentrantLock reentrantLockB = new ReentrantLock();

        final CountDownLatch countDownLatch = new CountDownLatch(1);
    
        final Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
    
                System.out.println(Thread.currentThread().getId() + "获取a");
                try {
                    reentrantLockA.lockInterruptibly();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getId() + "进入a");
                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getId() + "获取b");
                try {
                    reentrantLockB.lockInterruptibly();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getId() + "进入b");
    
                reentrantLockB.unlock();
                    reentrantLockA.unlock();
                }
        });
        threadA.start();
        final Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
    
                System.out.println(Thread.currentThread().getId() + "获取b");
                try {
                    reentrantLockB.lockInterruptibly();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getId() + "进入b");
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getId() + "获取a");
                try {
                    reentrantLockA.lockInterruptibly();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getId() + "进入a");
                reentrantLockA.unlock();
                reentrantLockB.unlock();
            }
        });
        threadB.start();
    
        new Thread(new Runnable() {
            @Override
            public void run() {
                countDownLatch.countDown();
                System.out.println("threada interrupt");
                //threadA.interrupt();
                System.out.println("threadb interrupt");
                //threadB.interrupt();
                System.out.println("获取aaaaaa 前");
                reentrantLockA.lock();
                    System.out.println("获取aaaaaa");
                reentrantLockA.unlock();
            }
        }).start();
    
    }
    

    先注释掉interrupt的情况


    image.png

    取消注释后的情况


    image.png

    相关文章

      网友评论

          本文标题:sychronized vs lock interrupt

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