sychronized vs lock interrupt

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)