zoukankan      html  css  js  c++  java
  • 加锁问题,必须加锁在对象上或方法上,加在基本数据类型上无效

    如下代码:
    运行结果:

    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;
        }
    }
  • 相关阅读:
    Prometheus服务发现
    持久化查询
    PromQL进阶
    PromQL基础
    Prometheus概述
    监控系统概念
    zabbix5x解决中文字体问题
    allure 插件新手 demo
    关于时间复杂度~
    IIS发布网站Microsoft JET Database Engine 错误 '80004005'的解决办法,基于Access数据库
  • 原文地址:https://www.cnblogs.com/IamThat/p/4540774.html
Copyright © 2011-2022 走看看