zoukankan      html  css  js  c++  java
  • synchronized用static修饰和不用static修饰的区别,synchronized同步代码块this和类的Class对象作为锁的区别。

    看下面的例子:

    package com.andy.juc;
    
    public class SynchronizedTest {
        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    sub();
                }
            }).start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            SynchronizedTest.add();
        }
    
    
        public static synchronized void add(){
            System.out.println("hello add");
        }
    
        public static synchronized void sub()  {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello sub");
        }
    }

    一个是实例方法,一个是静态方法

    package com.andy.juc;
    
    public class SynchronizedTest {
        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    sub();
                }
            }).start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new SynchronizedTest().add();
            
        }
    
    
        public  synchronized void add(){
            System.out.println("hello add");
        }
    
        public static synchronized void sub()  {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello sub");
        }
    }

    创建两个对象,分别调用自己的实例方法,不会有并发问题。

    package com.andy.juc;
    
    public class SynchronizedTest {
        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    new SynchronizedTest().sub();
                }
            }).start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new SynchronizedTest().add();
    
        }
    
    
        public  synchronized void add(){
            System.out.println("hello add");
        }
    
        public  synchronized void sub()  {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello sub");
        }
    }

    同一个对象,锁资源相互排斥

    package com.andy.juc;
    
    public class SynchronizedTest {
        public static void main(String[] args) {
            SynchronizedTest synchronizedTest = new SynchronizedTest();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronizedTest.sub();
                }
            }).start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronizedTest.add();
    
        }
    
    
        public  synchronized void add(){
            System.out.println("hello add");
        }
    
        public  synchronized void sub()  {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("hello sub");
        }
    }

    同步代码块,用类的Class对象来加锁,互斥

    package com.andy.juc;
    
    public class SynchronizedTest {
        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    new SynchronizedTest().sub();
                }
            }).start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            new SynchronizedTest().add();
    
        }
    
    
        public   void add(){
            synchronized(SynchronizedTest.class){
                System.out.println("hello add");
            }
        }
    
        public  void sub()  {
            synchronized(SynchronizedTest.class){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("hello sub");
            }
    
        }
    }
  • 相关阅读:
    vue中 $event 的用法--获取当前父元素,子元素,兄弟元素
    vue的通信方式(二)---祖父孙三个级别的之间的隔代通信
    js获取当前页面url信息
    解决Vue刷新一瞬间出现样式未加载完或者出现Vue代码问题
    Java POI读取excel中数值精度损失
    Java读取Excel数值内容带.0或变科学计数法的解决办法
    Java专业术语
    jdbc连接数据库
    阿里云linux配置ftp服务
    mybatis批量更新表setting parameters 错误
  • 原文地址:https://www.cnblogs.com/mkl34367803/p/14761275.html
Copyright © 2011-2022 走看看