• 加锁问题,必须加锁在对象上或方法上,加在基本数据类型上无效


    如下代码:
    运行结果:

    Thread-0 holds the locktrue
    Exception in thread "Thread-0" java.lang.IllegalMonitorStateException
    at java.lang.Object.notify(Native Method)
    at blockthread.DeadLock$Thread1.run(DeadLock.java:24)
    main holds the lockfalse
    Thread-0:99
    Thread-1 holds the lockfalse
    Thread-1:100

    失败原因是: synchronized 只能同步对象或者方法,如果在基本数据类型上给它加锁,synchronized 无效。



    package
    blockthread; public class DeadLock { public static Integer i=100; public static void main(String[] args) { Thread1 t1=new Thread1(); Thread2 t2=new Thread2(); t1.start(); t2.start(); System.out.println(Thread.currentThread().getName()+" holds the lock"+Thread.holdsLock(i)); } static class Thread1 extends Thread{ public void run(){ while(true){ synchronized(i){ System.out.println(this.getName()+" holds the lock"+Thread.holdsLock(i)); if(i>0) i--; System.out.println(this.getName()+":"+i); i.notify(); try { i.wait(); System.out.println("线程1 等待"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //i.notify(); } } } static class Thread2 extends Thread{ public void run(){ while(true){ synchronized(i){ System.out.println(this.getName()+" holds the lock"+Thread.holdsLock(i)); if(i<100) i++; System.out.println(this.getName()+":"+i); i.notify(); try { i.wait(); System.out.println("线程2等待"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //i.notify(); } } } }

    代码改为如下: 正常运行。

    package blockthread;
    
    public class DeadLock {
        
        
        public static void main(String[] args) {
            Instance instance=new Instance();
            Thread1 t1=new Thread1(instance);
            Thread2 t2=new Thread2(instance);
            t1.start();
            t2.start();
            System.out.println(Thread.currentThread().getName()+" holds the lock"+Thread.holdsLock(instance));
        }
        
        static class Thread1 extends Thread{
            private Instance instance;
            
            public Thread1(Instance instance){
                this.instance=instance;
            }
            
            public void run(){
                while(true){
                    synchronized(instance){
                        System.out.println(this.getName()+" holds the lock"+Thread.holdsLock(instance));
                        if(instance.getI()>0){
                            int i=instance.getI();
                            i--;
                        }
                        System.out.println(this.getName()+":"+instance.getI());
                        instance.notify();
                        try {
                            instance.wait();
                            System.out.println("线程1 等待");
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    //i.notify();
                }
            }
        }
        
        static class Thread2 extends Thread{
    private Instance instance;
            
            public Thread2(Instance instance){
                this.instance=instance;
            }
            public void run(){
                while(true){
                    synchronized(instance){
                        System.out.println(this.getName()+" holds the lock"+Thread.holdsLock(instance));
                        if(instance.getI()<100){
                            int i=instance.getI();
                            i++;
                        }
                        System.out.println(this.getName()+":"+instance.getI());
                        instance.notify();
                        try {
                            instance.wait();
                            System.out.println("线程2等待");
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    //i.notify();
                }
            }
        }
        
        
    
    }
    class Instance{
        private int i=100;
    
        public int getI() {
            return i;
        }
    
        public void setI(int i) {
            this.i = i;
        }
    }
  • 相关阅读:
    怎么往mac中finder个人收藏里添加文件夹
    UIView 动画
    添加.pch文件
    声明属性的关键字
    创建app前的环境配置/AppIcon/启动图片
    修改动画的旋转支点
    实现自定义xib和storyboard的加载,
    Quartz2D绘图 及实例:下载进度
    帧动画
    在职研究生第一单元第二单元第三单元第四单元是什么?
  • 原文地址:https://www.cnblogs.com/IamThat/p/4540774.html
Copyright © 2020-2023  润新知