synchronized

对于一个特定对象而言,其所有synchronized方法共享同一个锁。

import java.util.concurrent.*;

class SynchronizedDemo{
    private int a = 0;
    private int b = 0;
    public synchronized void add() {
        try{
            System.out.println("11111111: " + a);
            a++;
            System.out.println("22222222: " + a);
            TimeUnit.MILLISECONDS.sleep(300);
            a++;
            System.out.println("33333333: " +  a);
        } catch (InterruptedException e) {
            System.out.println("interrupted!");
        }
    }

    public synchronized void sub() {
        try{
            System.out.println("000011111111: " + b);
            b--;
            System.out.println("000022222222: " + b);
            TimeUnit.MILLISECONDS.sleep(300);
            b--;
            System.out.println("000033333333: " + b);
        } catch (InterruptedException e) {
            System.out.println("0000interrupted!");
        }
    }
}

class TestAdd implements Runnable{
    SynchronizedDemo demo = null;
    public TestAdd(SynchronizedDemo demo) {
        this.demo = demo;
    }
    public void run() {
        demo.add();
    }
}

class TestSub implements Runnable{
    SynchronizedDemo demo = null;
    public TestSub(SynchronizedDemo demo) {
        this.demo = demo;
    }
    public void run() {
        demo.sub();
    }
}

public class TestSynchronized {
    public static void main(String... args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        SynchronizedDemo demo = new SynchronizedDemo();
        for(int i = 0; i<5; i++) {
            exec.execute(new TestAdd(demo));
        }
        for(int i = 0; i<5; i++) {
            exec.execute(new TestSub(demo));
        }
        exec.shutdown();
    }
}


Output:
11111111: 0
22222222: 1
33333333: 2
000011111111: 0
000022222222: -1
000033333333: -2
000011111111: -2
000022222222: -3
000033333333: -4
000011111111: -4
000022222222: -5
000033333333: -6
000011111111: -6
000022222222: -7
000033333333: -8
000011111111: -8
000022222222: -9
000033333333: -10
11111111: 2
22222222: 3
33333333: 4
11111111: 4
22222222: 5
33333333: 6
11111111: 6
22222222: 7
33333333: 8
11111111: 8
22222222: 9
33333333: 10

上例中,只要锁没有释放,其他线程都会被阻塞,即使拿到锁的线程sleep。

import java.util.concurrent.*;

class SynchronizedDemo{
    private int a = 0;
    private int b = 0;
    public void add() {
        synchronized(this) {
        try{
            System.out.println("11111111: " + a);
            a++;
            System.out.println("22222222: " + a);
            TimeUnit.MILLISECONDS.sleep(300);
            a++;
            System.out.println("33333333: " +  a);
        } catch (InterruptedException e) {
            System.out.println("interrupted!");
        }
        }
    }

    public void sub() {
        //synchronized(this) {
        try{
            System.out.println("000011111111: " + b);
            b--;
            System.out.println("000022222222: " + b);
            TimeUnit.MILLISECONDS.sleep(300);
            b--;
            System.out.println("000033333333: " + b);
        } catch (InterruptedException e) {
            System.out.println("0000interrupted!");
        }
        //}
    }
}

class TestAdd implements Runnable{
    SynchronizedDemo demo = null;
    public TestAdd(SynchronizedDemo demo) {
        this.demo = demo;
    }
    public void run() {
        demo.add();
    }
}

class TestSub implements Runnable{
    SynchronizedDemo demo = null;
    public TestSub(SynchronizedDemo demo) {
        this.demo = demo;
    }
    public void run() {
        demo.sub();
    }
}

public class TestSynchronized {
    public static void main(String... args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        SynchronizedDemo demo = new SynchronizedDemo();
        for(int i = 0; i<5; i++) {
            exec.execute(new TestAdd(demo));
        }
        for(int i = 0; i<5; i++) {
            exec.execute(new TestSub(demo));
        }
        exec.shutdown();
    }
}


Output:
11111111: 0
22222222: 1
000011111111: 0
000022222222: -1
000011111111: -1
000022222222: -2
000011111111: -2
000022222222: -3
000011111111: -3
000011111111: -3
000022222222: -5
000022222222: -4
33333333: 2
11111111: 2
22222222: 3
000033333333: -6
000033333333: -7
000033333333: -8
000033333333: -9
000033333333: -10
33333333: 4
11111111: 4
22222222: 5
33333333: 6
11111111: 6
22222222: 7
33333333: 8
11111111: 8
22222222: 9
33333333: 10

results matching ""

    No results matching ""