A-A+

Java-Synchronized用法(1)

2018年12月16日 技术, 默认 暂无评论 阅读 2,182 次

Synchronized主要有以下几种用法:

1. 加在方法上:这里包括加在实例方法上、加在静态方法上。

2. 方法块:这里包括锁定this、锁定Class、锁定实例对象、锁定静态对象。

今天主要通过例子演示一下第一个用法:加在实例方法上和加在静态方法上。

首先是加在实例方法上

测试代码如下:

 1import org.junit.Test;
2
3import java.util.Scanner;
4
5public class ThreadTest {
6
7    public synchronized void printThis(String input) {
8        System.out.println(input + System.currentTimeMillis());
9        try {
10            Thread.sleep(3000);
11        } catch (InterruptedException e) {
12            e.printStackTrace();
13        }
14        System.out.println("3秒后的" + input + System.currentTimeMillis());
15    }
16
17    private void waitToQuite() {
18        new Scanner(System.in).next();
19    }
20
21    //region 测试方法
22
23    /**
24     * 锁定实例方法,同一个对象调用时等待
25     */

26    @Test
27    public void test1() {
28
29        final ThreadTest test = new ThreadTest();
30
31        new Thread(() -> {
32            System.out.println("第一个线程已启动" + System.currentTimeMillis());
33            test.printThis("我是第一个线程");
34        }).start();
35
36        new Thread(() -> {
37            System.out.println("第二个线程已启动" + System.currentTimeMillis());
38            test.printThis("我是第二个线程");
39        }).start();
40
41        waitToQuite();
42    }
43
44    /**
45     * 锁定实例方法,不同对象调用不需要等待
46     */

47    @Test
48    public void test2() {
49        final ThreadTest test1 = new ThreadTest();
50        final ThreadTest test2 = new ThreadTest();
51
52        new Thread(new Runnable() {
53            @Override
54            public void run() {
55                System.out.println("第一个线程已启动");
56                test1.printThis("我是第一个线程");
57            }
58        }).start();
59
60        new Thread(new Runnable() {
61            @Override
62            public void run() {
63                System.out.println("第二个线程已启动");
64                test2.printThis("我是第二个线程");
65            }
66        }).start();
67
68        waitToQuite();
69    }
70
71    //endregion
72}

测试结果如下:


锁定实例方法,同一个对象,被调用时等待

锁定实例方法,不同对象,调用时不需要等待

其次是加在静态方法上

测试代码如下:

 1import org.junit.Test;
2
3import java.util.Scanner;
4
5public class ThreadTest {
6
7    public synchronized static void printStatic(String input) {
8        System.out.println(input + System.currentTimeMillis());
9        try {
10            Thread.sleep(3000);
11        } catch (InterruptedException e) {
12            e.printStackTrace();
13        };
14        System.out.println("3秒后的" + input + System.currentTimeMillis());
15    }
16
17    public void callPrintStatic(String input) {
18        printStatic(input);
19    }
20
21    private void waitToQuite() {
22        new Scanner(System.in).next();
23    }
24
25    //region 测试方法
26
27    /**
28     * 锁定静态方法,相同对象调用时等待
29     */

30    @Test
31    public void test1() {
32
33        final ThreadTest test1 = new ThreadTest();
34
35        new Thread(() -> {
36            System.out.println("第一个线程已启动");
37            test1.callPrintStatic("我是第一个线程");
38        }).start();
39
40        new Thread(() -> {
41            System.out.println("第二个线程已启动");
42            test1.callPrintStatic("我是第二个线程");
43        }).start();
44
45        waitToQuite();
46    }
47
48    /**
49     * 锁定静态方法,不同对象调用时等待
50     */

51    @Test
52    public void test2() {
53
54        final ThreadTest test1 = new ThreadTest();
55        final ThreadTest test2 = new ThreadTest();
56
57        new Thread(() -> {
58            System.out.println("第一个线程已启动");
59            test1.callPrintStatic("我是第一个线程");
60        }).start();
61
62        new Thread(() -> {
63            System.out.println("第二个线程已启动");
64            test2.callPrintStatic("我是第二个线程");
65        }).start();
66
67        waitToQuite();
68    }
69
70    //endregion
71}

测试结果如下:


锁定实例方法,相同对象,调用时需要等待

锁定实例方法,相同对象,调用时需要等待

觉的不错?可以关注我的公众号↑↑↑

给我留言

Copyright © 字痕随行 保留所有权利.   Theme  Ality

用户登录

分享到: