zoukankan      html  css  js  c++  java
  • java并发编程之美-笔记

    • 测试wait的interrupt
    public class Test {
        static Object object = new Object();
    
        public static void main(String[] args) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println("--begin--");
                        synchronized (object){
                            object.wait();
                        }
                        System.out.println("--end--");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
                }
            });
            thread.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("begin interrupt thread");;
            thread.interrupt();
            System.out.println("--end interrupt thread");
        }
    }
    

    测试wait()方法

    public class Test {
        private static volatile Object resourceA = new Object();
        private static volatile Object resourceB = new Object();
    
        public static void main(String[] args) throws InterruptedException {
            Thread threadA = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (resourceA){
                        System.out.println("threadA get resourceA lock");
                        synchronized (resourceB){
                            System.out.println("threadA get resourceB lock");
                            System.out.println("threadA release resourceA lock");
                            try {
                                resourceA.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });
            Thread threadB =new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                        synchronized (resourceA){
                            System.out.println("threadB get resourceA lock");
                            System.out.println("threadB try get resourceB lock");
                            synchronized (resourceB){
                                System.out.println("threadB get resourceB lock");
                                System.out.println("threadB release resourceA lock");
                                resourceA.wait();
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
    
            threadA.start();
            threadB.start();
    
            threadA.join();
            threadB.join();
        }
    }
    

    输出结果:
    threadA get resourceA lock
    threadA get resourceB lock
    threadA release resourceA lock
    threadB get resourceA lock
    threadB try get resourceB lock
    A资源使用后释放了锁,B资源使用后没有释放锁,最终导致B资源的死锁

    • 测试notify方法
    public class Test {
       private static volatile Object resourceA = new Object();
    
        public static void main(String[] args) throws InterruptedException {
            Thread threadA = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (resourceA){
                    try {
                        System.out.println("threadA get resourceA lock");
                        System.out.println("threadA begin wait");
                        resourceA.wait();
                        System.out.println("threadA end wait");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                    }
                }
            });
            Thread threadB = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (resourceA){
                        System.out.println("threadB get resourceA lock");
                        System.out.println("threadB begin wait");
                        try {
                            resourceA.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("threadB end wait");
                    }
                }
            });
            Thread threadC = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (resourceA){
                        System.out.println("threadC begin notify");
                        resourceA.notify();
                        System.out.println("threadC end notify");
                    }
                }
            });
    
            threadA.start();
            threadB.start();
            Thread.sleep(2000);
            threadC.start();
    
            threadA.join();
            threadB.join();
            threadC.join();
            System.out.println("start over");
        }
    }
    

    threadA get resourceA lock
    threadA begin wait
    threadB get resourceA lock
    threadB begin wait
    threadC begin notify
    threadC end notify
    threadA end wait
    当A和B都执行到wait方法时,执行c的notify方法,然后A线程被唤醒,执行到end wait
    如果C改成notifyAll方法,则返回:
    threadA get resourceA lock
    threadA begin wait
    threadB get resourceA lock
    threadB begin wait
    threadC begin notify
    threadC end notify
    threadB end wait
    threadA end wait
    start over
    两线程都会执行到结束

    Unsate的使用

    public class TestUnsafe {
        static final Unsafe unsafe ;
        static final long stateOffset;
        private volatile long state = 0;
    
        static {
            try {
                Field field = Unsafe.class.getDeclaredField("theUnsafe");
                field.setAccessible(true);
                unsafe = (Unsafe)field.get(null);
                stateOffset = unsafe.objectFieldOffset(TestUnsafe.class.getDeclaredField("state"));
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
                throw new Error(e);
            }
    
        }
    
        public static void main(String[] args) {
            TestUnsafe testUnsafe = new TestUnsafe();
            Boolean success = unsafe.compareAndSwapInt(testUnsafe,stateOffset,0,1);
            System.out.println(success);
        }
    }
    
    
  • 相关阅读:
    计算机图形学之光栅图形学——反走样算法简介
    计算机图形学之光栅图形学——区域填充算法简介
    计算机图形学之光栅图形学——多边形的扫描转换算法简介
    计算机图形学——光栅图形学直线算法简介
    Kendall tau距离(即两个内容相同的数组中逆序数对的数量)(算法》P220 第2.5.3.2小节)
    自顶向下归并排序的应用——链表的随机化
    双调数组中的二分查找
    根据离散概率随机返回数组中的项
    字符串解析
    找出只出现一次的整数
  • 原文地址:https://www.cnblogs.com/Baronboy/p/13532587.html
Copyright © 2011-2022 走看看