synchronized关键字(二):案例分析

前言

synchronized系列文章
synchronized关键字(一):实现原理

学完前一篇:synchronized关键字(一):实现原理 之后,我们对synchronized有了深入的理解,理解之后肯定就是运用了。所以本篇主要讲解synchronized关键字的几种实际应用场景,总结了一下,synchronized关键字的主要使用场景,从易到难,大致分为以下几种情况

案例

两个线程同时访问同一个对象的非静态同步方法
public class SynchronizedDemo1 implements Runnable {

    public static void main(String[] args) {
    	// 同一个对象
        SynchronizedDemo1 lock = new SynchronizedDemo1();
        Thread thread1 = new Thread(lock, "thread-1");
        Thread thread2 = new Thread(lock, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        display();
    }

    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

执行结果

thread-1 -> 1569765814781 -> display()
thread-1 -> 1569765816781 -> finished
thread-2 -> 1569765816781 -> display()
thread-2 -> 1569765818782 -> finished

注意输出时间,thread-1输出display()后休眠了2秒,输出finished,马上thread-2输出display(),休眠2秒后输出finished。说明两个线程同时访问同一个对象的非静态同步方法时,同步方法是串行的,即synchronized锁有效

两个线程同时访问两个对象的非静态同步方法
public class SynchronizedDemo2 implements Runnable {

    public static void main(String[] args) {
        // 不同对象
        SynchronizedDemo2 lock1 = new SynchronizedDemo2();
        SynchronizedDemo2 lock2 = new SynchronizedDemo2();
        Thread thread1 = new Thread(lock1, "thread-1");
        Thread thread2 = new Thread(lock2, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        display();
    }

    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569766073360 -> display()
thread-2 -> 1569766073361 -> display()
thread-1 -> 1569766075362 -> finished
thread-2 -> 1569766075362 -> finished

从输出结果可以看出,thread-1输出display()后,thread-2马上输出display(),然后各自休眠2秒,输出finished。可以说明两个线程同时访问两个对象的同步方法时,代码不是串行的,即synchronized锁无效(使用方式错误)

两个线程同时访问synchronized静态方法
public class SynchronizedDemo3 implements Runnable {

    public static void main(String[] args) {
        // 不同对象
        SynchronizedDemo3 lock1 = new SynchronizedDemo3();
        SynchronizedDemo3 lock2 = new SynchronizedDemo3();
        Thread thread1 = new Thread(lock1, "thread-1");
        Thread thread2 = new Thread(lock2, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        display();
    }
    
    // 静态方法
    private static synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569766374886 -> display()
thread-1 -> 1569766376887 -> finished
thread-2 -> 1569766376888 -> display()
thread-2 -> 1569766378888 -> finished

thread-1输出display()后休眠了2秒,输出finished,马上thread-2输出display(),休眠2秒后输出finished。说明两个线程同时访问synchronized静态方法时,同步方法是串行的(即使传入两个不同的对象),即synchronized锁有效

两个线程同时访问非静态同步方法与普通方法
public class SynchronizedDemo4 implements Runnable {

    public static void main(String[] args) {
        SynchronizedDemo4 lock1 = new SynchronizedDemo4();
        SynchronizedDemo4 lock2 = new SynchronizedDemo4();
        Thread thread1 = new Thread(lock1, "thread-1");
        Thread thread2 = new Thread(lock2, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        if (Objects.equals(Thread.currentThread().getName(), "thread-1")) {
            // thread-1访问非静态同步方法
            display();
        } else {
            // thread-2访问普通方法
            show();
        }
    }

    // 非静态同步方法
    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }

    // 普通方法
    private void show() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> show()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569766853881 -> display()
thread-2 -> 1569766853882 -> show()
thread-1 -> 1569766855883 -> finished
thread-2 -> 1569766855883 -> finished

thread-1输出非静态同步方法display()后,thread-2马上输出了普通方法中的show(),然后各自休眠2秒后输出finished。说明两个线程同时访问非静态同步方法与普通方法时,非静态同步方法与普通方法不是串行的,即synchronized锁无效

两个线程同时访问同一个对象的不同非静态synchronized方法
public class SynchronizedDemo5 implements Runnable {

    public static void main(String[] args) {
        // 同一个对象
        SynchronizedDemo5 lock = new SynchronizedDemo5();
        Thread thread1 = new Thread(lock, "thread-1");
        Thread thread2 = new Thread(lock, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        if (Objects.equals(Thread.currentThread().getName(), "thread-1")) {
            // thread-1访问非静态同步方法
            display();
        } else {
            // thread-2访问非静态同步方法
            show();
        }
    }

    // 非静态同步方法
    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }

    // 非静态同步方法
    private synchronized void show() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> show()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569767264895 -> display()
thread-1 -> 1569767266896 -> finished
thread-2 -> 1569767266896 -> show()
thread-2 -> 1569767268897 -> finished

thread-1输出display()后休眠了2秒,输出finished,马上thread-2输出show(),休眠2秒后输出finished。说明两个线程同时访问同一个对象的不同非静态synchronized方法,同步方法是串行的,即synchronized锁有效

两个线程同时访问静态synchronized方法和非静态synchronized方法
public class SynchronizedDemo6 implements Runnable {

    public static void main(String[] args) {
        SynchronizedDemo6 lock = new SynchronizedDemo6();
        Thread thread1 = new Thread(lock, "thread-1");
        Thread thread2 = new Thread(lock, "thread-2");

        thread1.start();
        thread2.start();
    }

    @Override
    public void run() {
        if (Objects.equals(Thread.currentThread().getName(), "thread-1")) {
            // thread-1访问非静态同步方法
            display();
        } else {
            // thread-2访问静态同步方法
            show();
        }
    }

    // 非静态同步方法
    private synchronized void display() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> display()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }

    // 静态同步方法
    private static synchronized void show() {
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> show()");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + " -> " + System.currentTimeMillis() + " -> finished");
    }
}

输出结果

thread-1 -> 1569767538794 -> display()
thread-2 -> 1569767538795 -> show()
thread-1 -> 1569767540796 -> finished
thread-2 -> 1569767540796 -> finished

thread-1输出非静态同步方法display()后,thread-2马上输出了静态同步方法中的show(),然后各自休眠2秒后输出finished。说明两个线程同时访问静态synchronized方法和非静态synchronized方法时,非静态同步方法与普通方法不是串行的,即synchronized锁无效

总结

以上几种情况,虽说看起来比较复杂,也比较绕。但是归根结底,只需要明确两点,就可以做到百战不殆。

  • 明确线程获取的是对象锁还是类锁
  • 同一把锁,在同一时刻最多被一个线程获取

上面六个案例,围绕这两点分析,相信会很好理解。

发布了52 篇原创文章 · 获赞 107 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Baisitao_/article/details/101719545
今日推荐