java中synchronized的关键字

java中每个对象都会有一个对象锁,而synchronized就是得到这个锁,看下面这个例子

import java.util.Random;
public class MyData{
    
    public synchronized void increment() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(200));
            } catch (Exception e) {
                e.printStackTrace();
            }
           System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
    
    public synchronized void decrement() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(200));
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
    
    public static void main(String[] args) {
        final MyData myData1 = new MyData();
       // final MyData myData2 = new MyData();
       new Thread(new Runnable() {
            @Override
            public void run() {
                myData1.increment();
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                myData1.decrement();
            }
        }).start();
    }
}

 

无论执行多少次都是有序的,两个线程操作的是同一个对象,第一个执行的线程得到了锁,第二个线程只能等第一个线程执行完了才能拿到锁,进入方法。

再看下面这个例子

import java.util.Random;
public class MyData{
    
    public synchronized void increment() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(200));
            } catch (Exception e) {
                e.printStackTrace();
            }
           System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
    
    public synchronized void decrement() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(200));
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
    
    public static void main(String[] args) {
        final MyData myData1 = new MyData();
        final MyData myData2 = new MyData();
       new Thread(new Runnable() {
            @Override
            public void run() {
                myData1.increment();
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                myData2.decrement();
            }
        }).start();
    }
}

执行的结果是无序的,两个对象,两把锁,故互不影响,各自执行各自的。

再来看看下面这个例子

import java.util.Random;
public class MyData{
    
    public synchronized void increment() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(200));
            } catch (Exception e) {
                e.printStackTrace();
            }
           System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
    
    public static synchronized void decrement() {
        for (int i = 0; i < 10; i++) {
            try {
                Thread.sleep(new Random().nextInt(200));
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ":" +i);
        }
    }
    
    public static void main(String[] args) {
        final MyData myData1 = new MyData();
      //  final MyData myData2 = new MyData();
       new Thread(new Runnable() {
            @Override
            public void run() {
                myData1.increment();
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                MyData.decrement();
            }
        }).start();
    }
}

结果也是无序的,原因和上面一样,static方法是属于Class对象的,故decrement方法锁的MyData.Class对象,而myData1.increment();锁的是myData1对象,互不干扰。

只需记住synchronized锁的是对象,每个对象有一把对象锁,拿到锁之后才能执行synchronized的方法

posted @ 2017-05-22 11:06  段少  阅读(209)  评论(0编辑  收藏  举报