zoukankan      html  css  js  c++  java
  • 线程同步synchronized,Class与Object

    synchronized (class):class类的同步,同步的时候会同步整个class

    synchronized (Object):Object的同步,只对其中的对象同步

    如下:对类B中的同步代码块的同步,对比之后放可明白

    synchronized (MyThread.class)

    例:

    A:类UnsafeSequence

    public class UnsafeSequence {
        private static int value;
        
        public int getValue()
        {
            synchronized (UnsafeSequence.class) {
                return value++;
            }
        }
        
        public int getValueA()
        {
            synchronized (this) {
                return value++;
            }
        }
    }

    B:类MyThread

    public class MyThread implements Runnable {
        private UnsafeSequence unsafe;
        private static int value;
        
        @Override
        public void run() {
            unsafe = new UnsafeSequence();
            for(int i = 0; i < 60; i++)
            {
                synchronized (MyThread.class) {
                    System.out.println(Thread.currentThread().getName() + "----in:");
                    System.out.println(Thread.currentThread().getName() + " thread run i = " + i + ";value = " + unsafe.getValue());
                    System.out.println(Thread.currentThread().getName() + "----out:" + value++);
                }
            }
        }

    }

    C:主函数

    public class TestMain {
        public static void main(String[] args) {
            MyThread target_1 = new MyThread();
            MyThread target_2 = new MyThread();
            Thread thread_1 = new Thread(target_1, "A");
            Thread thread_2 = new Thread(target_2, "B");
            thread_1.start();
            thread_2.start();
        }
    }

    结果如下:

    B----in:
    B thread run i = 0;value = 0
    B----out:0
    B----in:
    B thread run i = 1;value = 1
    B----out:1
    B----in:
    B thread run i = 2;value = 2
    B----out:2

    去掉B中的同步,或换为Object,结果如下:

    A----in:
    B----in:
    A thread run i = 0;value = 0
    B thread run i = 0;value = 1
    A----out:0
    B----out:1
    A----in:
    B----in:
    A thread run i = 1;value = 2
    B thread run i = 1;value = 3
    B----out:3
    B----in:
    A----out:2

    控制同步:

    public class MyThread implements Runnable {
        private UnsafeSequence unsafe;
        private static int value;
        
        @Override
        public void run() {
            synchronized (MyThread.class) {
                unsafe = new UnsafeSequence();
                for(int i = 0; i < 60; i++)
                {
                    System.out.println(Thread.currentThread().getName() + "----in:");
                    System.out.println(Thread.currentThread().getName() + " thread run i = " + i + ";value = " + unsafe.getValue());
                    System.out.println(Thread.currentThread().getName() + "----out:" + value++);
                }
            }
        }
    }

    我很小我很弱,我在慢慢成长!
  • 相关阅读:
    python os 用法(转)
    caffe for python
    day20-Python运维开发基础(装饰器 / 类中的方法 / 类的方法变属性)
    day19-Python运维开发基础(类的魔术方法)
    day18-Python运维开发基础(单继承 / 多继承 / 菱形继承、类的多态)
    day17-Python运维开发基础(类的封装 / 对象和类的相关操作、构造方法)
    day16-Python运维开发基础(os / os.path / shutil模块)
    Linux 创建网卡子接口
    day15-Python运维开发基础(json序列化模块、random / time / zipfile / OS模块函数)
    day14-Python运维开发基础(内置函数、pickle序列化模块、math数学模块)
  • 原文地址:https://www.cnblogs.com/lvzhanhui/p/lvzhanhui.html
Copyright © 2011-2022 走看看