jvm内置锁synchronized不能被中断
生活随笔
收集整理的這篇文章主要介紹了
jvm内置锁synchronized不能被中断
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很久沒看技術書籍了,今天看了一下《七周七并發模型》前面兩章講的java,寫的還是有深度的。看到了一個有demo,說jvm內置鎖synchronized是不能被中斷的。照著書上寫了個demo,驗證了一下,是不能被中斷
/*** @Author: * @Description: jdk內置鎖不能被中斷* @Date: Created in : 2018/10/4 下午11:34**/ public class Uninterruptible {private static final Object o1 = new Object();private static final Object o2 = new Object();public static void main(String[] args) throws InterruptedException {t1.start();t2.start();Thread.sleep(2000);System.out.println("before interrupt");t1.interrupt();t2.interrupt();System.out.println("end interrupt");t1.join();t2.join();}static Thread t1 = new Thread(() -> {synchronized (o1){try {System.out.println("start t1");Thread.sleep(1000);synchronized (o2){System.out.println("t1 lock o2");}} catch (InterruptedException e) {System.out.println("t1 interrupted");e.printStackTrace();}}});static Thread t2 = new Thread(() -> {synchronized (o2){try {System.out.println("start t2");Thread.sleep(1000);synchronized (o1){System.out.println("t2 lock o1");}} catch (InterruptedException e) {System.out.println("t2 intterrupted");e.printStackTrace();}}}); }?
轉載于:https://www.cnblogs.com/luckygxf/p/9743847.html
總結
以上是生活随笔為你收集整理的jvm内置锁synchronized不能被中断的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多进程实现生产者消费者
- 下一篇: 行业报告