zoukankan      html  css  js  c++  java
  • synchronized 锁的是方法还是对象还是类?测试实例

    package com.swt.lock;
    
    public class Test02 {
        public static void main(String[] args) throws InterruptedException {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    SyncMethod syncMethod = new SyncMethod();
                    syncMethod.sysn1();
                }
            });
            thread.start();
            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    SyncMethod syncMethod = new SyncMethod();
                    syncMethod.sysn2();
                }
            });
            thread.start();
            thread.join();
        }
    }
    
    class SyncMethod {
        public synchronized void sysn1() {
            for (int i = 0; i < 100; i++) {
                System.out.println("sysn1 " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public synchronized void sysn2() {
            for (int i = 0; i < 100; i++) {
                System.out.println("sysn2 " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

      输出结果可以看到,两个线程是交替执行。

    sysn1 0
    sysn2 0
    sysn2 1
    sysn1 1
    sysn2 2
    sysn1 2
    sysn2 3
    sysn1 3
    sysn2 4
    

      

    package com.swt.lock;
    
    public class Test02 {
        public static void main(String[] args) throws InterruptedException {
            SyncMethod syncMethod = new SyncMethod();
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    syncMethod.sysn1();
                }
            });
            thread.start();
            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    syncMethod.sysn2();
                }
            });
            thread.start();
            thread.join();
        }
    }
    
    class SyncMethod {
        public synchronized void sysn1() {
            for (int i = 0; i < 100; i++) {
                System.out.println("sysn1 " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public synchronized void sysn2() {
            for (int i = 0; i < 100; i++) {
                System.out.println("sysn2 " + i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

      输出结果可以看到,两个线程是顺序执行

    sysn1 0
    sysn1 1
    sysn1 2
    ...
    sysn1 98
    sysn1 99
    sysn2 0
    sysn2 1
    sysn2 2
    sysn2 3
    

      

  • 相关阅读:
    支持stl容器的gdb自定义命令
    Thrift辅助类,用于简化Thrift编程
    Linux上获取CPU Core个数的实现
    第54课 被遗弃的多重继承(下)
    第53课 被遗弃的多重继承(上)
    第52课 C++中的抽象类和接口
    第51课 C++对象模型分析(下)
    第50课 C++对象模型分析(上)
    第49课 多态的概念和意义
    第48课 同名覆盖引发的问题
  • 原文地址:https://www.cnblogs.com/song-wentao/p/7890191.html
Copyright © 2011-2022 走看看