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);
        }
    }
    
    
  • 相关阅读:
    ubuntu12.04 LTS 安装vmware 错误解决方法
    Linux 下的Bluetooth 架构 分类: Android驱动 20120316 11:07 492人阅读 评论(0) 收藏 举报 实战Linux Bluetooth编程(一) 协议栈概述
    通过DEFINE 生存config.h 配置
    Git的初次使用 ; Git常用命令查询 ; Git push ; Git pull 20111216 17:32 在介绍安装和简单使用前,先看一下百度百科中的简介吧: ———————————
    Android BCM4330 蓝牙BT驱动调试记录
    Linux的cpufreq(动态变频)技术
    高通平台android开发总结
    ssh 客户端配置文件
    Jprofile使用随笔_1_安装与监控linux
    服务器cpu占用100%,如何排查(java进程&sql)
  • 原文地址:https://www.cnblogs.com/Baronboy/p/13532587.html
Copyright © 2011-2022 走看看