• 多个线程多个锁


    /**
     * 关键字synchronized取得的锁都是对象锁!!!!!而不是把一段代码(方法)当做锁,
     * 所以代码中哪个线程先执行synchronized关键字的方法,哪个线程就持有该方法所属对象的锁(Lock),
     * 在静态方法上加synchronized关键字,表示锁定.class类,类一级别的锁(独占.class类)。
     */
    public class MultiThread {
        private int num = 0;
        public synchronized void printNum(String tag){
            try {
                if(tag.equals("a")){
                    num = 100;
                    System.out.println("tag a, set num over!");
                    Thread.sleep(1000);
                } else {
                    num = 200;
                    System.out.println("tag b, set num over!");
                }
                System.out.println("tag " + tag + ", num = " + num);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //注意观察run方法输出顺序
        public static void main(String[] args) {
            //俩个不同的对象
            final MultiThread m1 = new MultiThread();
            final MultiThread m2 = new MultiThread();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    m1.printNum("a");
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override 
                public void run() {
                    m2.printNum("b");
                }
            });        
            t1.start();
            t2.start();
        }
    }
    /**
     * 输出:tag a, set num over!
            tag b, set num over!
            tag b, num = 200
            tag a, num = 100
     */
    /**
     * 关键字synchronized取得的锁都是对象锁!!!!!而不是把一段代码(方法)当做锁,
     * 所以代码中哪个线程先执行synchronized关键字的方法,哪个线程就持有该方法所属对象的锁(Lock),
     * 在静态方法上加synchronized关键字,表示锁定.class类,类一级别的锁(独占.class类)。
     */
    public class MultiThread {
        private static int num = 0;
        public synchronized static void printNum(String tag){
            try {
                if(tag.equals("a")){
                    num = 100;
                    System.out.println("tag a, set num over!");
                    Thread.sleep(1000);
                } else {
                    num = 200;
                    System.out.println("tag b, set num over!");
                }
                System.out.println("tag " + tag + ", num = " + num);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //注意观察run方法输出顺序
        public static void main(String[] args) {
            //俩个不同的对象
            final MultiThread m1 = new MultiThread();
            final MultiThread m2 = new MultiThread();
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    m1.printNum("a");
                }
            });
            Thread t2 = new Thread(new Runnable() {
                @Override 
                public void run() {
                    m2.printNum("b");
                }
            });        
            t1.start();
            t2.start();
        }
    }
    /**
     * 输出:tag a, set num over!
            tag a, num = 100
            tag b, set num over!
            tag b, num = 200
     */
  • 相关阅读:
    什么是Netflix Feign?它的优点是什么?
    Spring Boot 自动配置原理是什么?
    springcloud断路器作用?
    什么是SpringCloudConfig?
    find命令查找包含指定内容的文件
    @PostConstruct使用总结
    @Retention 注解的作用
    SpringBoot自定义Condition注解
    Spring Boot 入门
    SpringBoot +MSSQL
  • 原文地址:https://www.cnblogs.com/fengmingyue/p/6678426.html
Copyright © 2020-2023  润新知