synchronized与volatile关键字

2018-04-15  本文已影响0人  black_toast

volatile

  1. volatile保证其他线程对这个变量操作时是立即可见的,即操作的是从内存中读取的最新值
  2. 无法保证原子性
  3. 只能修饰变量
public class Test {
    private volatile int count;
    public void increase() {
        count++;
        System.out.println("----" + count);
    }
    public static void main(String[] args) throws Exception {
        Test test = new Test();
        
        for (int i = 0; i < 10; i++) {
            new Thread() {
                public void run() {
                    for (int j = 0; j < 10; j++) {
                        test.increase();        
                    }
                };
            }.start();
        }
    }
}
控制台输出
class Singleton{
    private volatile static Singleton instance = null;
 
    private Singleton() {}
 
    public static Singleton getInstance() {
        if(instance==null) {
            synchronized (Singleton.class) {
                if(instance==null)
                    instance = new Singleton();
            }
        }
        return instance;
    }
}

synchronized

  1. 保证原子性
  2. 即可修饰变量也可修饰方法
  3. 会阻塞线程

synchronized非静态方法

public class Test {
    public synchronized void increase1() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("increase1---->" + i);
        }
    }
    
    public synchronized void increase2() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("increase2---->" + i);
        }
    }
    
    public static void main(String[] args) throws Exception {
        Test test = new Test();
        

            new Thread() {
                public void run() {
                    test.increase1();
                };
            }.start();
            new Thread() {
                public void run() {
                    test.increase2();
                };
            }.start();
    }
}
控制台输出

如果一个对象有多个synchronized方法,多个线程同时调用该对象的方法,将会同步执行,即同时只能有一个synchronized方法被调用,其他调用将被阻塞直至该方法执行完

synchronized静态方法

懒。。 直接给结论了

synchronized静态方法和非静态方法的区别在于给方法上锁的对象不一样,非静态方法是给调用的对象上锁,静态方法是给类的Class对象上锁

synchronized块

public class Test {
    public void increase1() {
        System.out.println("increase1----------> start");
        synchronized (this) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("increase1---->" + i);
            }
        }
        System.out.println("increase1----------> end");
    }
    
    public void increase2() {
        System.out.println("increase2----------> start");
        synchronized(this) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("increase2---->" + i);
            }
        }
        System.out.println("increase2----------> end");
    }
    
    public static void main(String[] args) throws Exception {
        Test test = new Test();
        

            new Thread() {
                public void run() {
                    test.increase1();
                };
            }.start();
            new Thread() {
                public void run() {
                    test.increase2();
                };
            }.start();
    }
}
控制台输出

synchronized方法是控制同时只能有一个线程执行synchronized方法;synchronized块是控制同时只能有一个线程执行synchronized块中的内容

上一篇 下一篇

猜你喜欢

热点阅读