synchronized关键字

2018-12-22  本文已影响0人  好奇害死猫o

1.synchronized锁的是什么

     synchronized可以锁定this、临界资源、Class类对象。

/***
 *  synchronized 关键字对某个对象加锁
 */
public class Test {

    private int count = 10;
    private Object o = new Object();
    public void m(){
        /**
         * 任何线程要执行以下代码,必须拿到对象o的锁
         */
        synchronized (o){
            count --;
            System.out.println(Thread.currentThread().getName() + " count = " + count);
        }
    }

}


/***
 * synchronized关键字
 * 对某个对象加锁
 */
public class Test {
    private int count = 10;
    public void m(){
        /**
         * 任何对象要想执行以下代码,必须拿到this对象的锁
         */
        synchronized (this){
            count--;
            System.out.println(Thread.currentThread().getName() + " count = " + count);
        }
    }
    /**
     *  等同于在方法的代码执行时要synchronized(this)
     */
    public synchronized void m1(){
        count--;
        System.out.println(Thread.currentThread().getName() + " count = " + count);
    }
}

/**
 * synchronized关键字
 * 对某个对象加锁
 */
public class Test {

   public static int count = 10;

    /***
     *  对静态增加synchronized关键字等同于synchronized (Test.class) 锁住当前类的Class对象
     */
   public synchronized static void m(){
       count--;
       System.out.println(Thread.currentThread().getName() + " count = " + count);
   }

   public static void m1(){
       synchronized (Test.class){
           count--;
           System.out.println(Thread.currentThread().getName() + " count = " + count);
       }
   }

}

2.synchronized 关键字的作用

首先先分析以下程序的输出

public class T implements Runnable {

    private int count = 10;
    
    public /*synchronized*/ void run() { 
        count--;
        System.out.println(Thread.currentThread().getName() + " count = " + count);
    }
    
    public static void main(String[] args) {
        T t = new T();
        for(int i=0; i<5; i++) {
            new Thread(t, "THREAD" + i).start();
        }
    }
    
}

输出如下:

THREAD4 count = 7
THREAD3 count = 7
THREAD2 count = 6
THREAD1 count = 5

五个线程访问t对象中的count属性,根据结果可以看出多线程情况下出现了数据不一致的情况。

加上synchronized 关键字之后再次运行

public class T implements Runnable {

    private int count = 10;
    
    public synchronized void run() {
        count--;
        System.out.println(Thread.currentThread().getName() + " count = " + count);
    }
    
    public static void main(String[] args) {
        T t = new T();
        for(int i=0; i<5; i++) {
            new Thread(t, "THREAD" + i).start();
        }
    }
    
}

运行结果

THREAD0 count = 9
THREAD1 count = 8
THREAD4 count = 7
THREAD3 count = 6
THREAD2 count = 5

synchronized 的代码块是一个原子操作,不可分。

3.同步和非同步方法是否可以同时调用?

public class T {
    public synchronized void m1() { 
        System.out.println(Thread.currentThread().getName() + " m1 start...");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " m1 end");
    }
    
    public void m2() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " m2 ");
    }
    public static void main(String[] args) {
        T t = new T();
        new Thread(t::m1, "t1").start();
        new Thread(t::m2, "t2").start();
    }
}

运行结果如下

t1 m1 start...
t2 m2 
t1 m1 end

在一个synchronize的方法执行之中非同步方法是可以执行的,m2执行的过程总不需要申请this对象的锁。

上一篇下一篇

猜你喜欢

热点阅读