锁的不同情况

PPG007 ... 2021-12-28 About 2 min

# 锁的不同情况

# 标准情况

public class EightLock {
    public static void main(String[] args) throws InterruptedException {
        先输出A再输出B
        Data data = new Data();
        new Thread(()->{
            data.a();
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            data.b();
        }).start();
    }
}
class Data {
    public synchronized void a(){
        System.out.println("A");
    }
    public synchronized void b(){
        System.out.println("B");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# a() 方法延迟 4 秒

synchronized 锁的是方法调用者,两个方法是同一个锁,谁先拿到谁执行。

class Data {
    public synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("A");
    }
    public synchronized void b(){
        System.out.println("B");
    }
}
1
2
3
4
5
6
7
8
9

# 增加一个普通方法

public class EightLock {
    public static void main(String[] args) throws InterruptedException {
        // 输出:C A
        Data data = new Data();
        new Thread(()->{
            try {
                data.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            data.c();
        }).start();
    }
}
class Data {
    public synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("A");
    }
    public synchronized void b(){
        System.out.println("B");
    }
    // 非同步方法,不受锁的影响
    public void c(){
        System.out.println("C");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 两个对象,两个同步方法

public class EightLock {
    public static void main(String[] args) throws InterruptedException {
        // 两个对象,两个调用者,两把锁
        // 输出:B A
        Data data = new Data();
        Data data1 = new Data();
        new Thread(()->{
            try {
                data.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            data1.b();
        }).start();
    }
}
class Data {
    public synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("A");
    }
    public synchronized void b(){
        System.out.println("B");
    }

    public void c(){
        System.out.println("C");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 增加两个静态同步方法,只有一个对象

public class EightLock {
    public static void main(String[] args) throws InterruptedException {
        // 输出:A B
        Data data = new Data();
        Data data1 = new Data();
        new Thread(()->{
            try {
                data.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            data.b();
        }).start();
    }
}
class Data {
    // 静态方法在类加载时就被加载,锁的是Class对象,Class对象全局唯一
    public static synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("A");
    }
    public static synchronized void b(){
        System.out.println("B");
    }

    public void c(){
        System.out.println("C");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

# 两个对象,两个静态同步方法

public class EightLock {
    public static void main(String[] args) throws InterruptedException {
        // 输出:A B
        Data data = new Data();
        Data data1 = new Data();
        new Thread(()->{
            try {
                data.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            data1.b();
        }).start();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 一个静态同步方法,一个普通同步方法,一个对象

public class EightLock {
    public static void main(String[] args) throws InterruptedException {
        // 输出:B A
        Data data = new Data();
        Data data1 = new Data();
        new Thread(()->{
            try {
                data.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            data.b();
        }).start();
    }
}
class Data {
    public static synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("A");
    }
    public synchronized void b(){
        System.out.println("B");
    }

    public void c(){
        System.out.println("C");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# 一个静态同步方法,一个普通同步方法,两个对象

public class EightLock {
    public static void main(String[] args) throws InterruptedException {
        // 输出:B A
        Data data = new Data();
        Data data1 = new Data();
        new Thread(()->{
            try {
                data.a();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            data1.b();
        }).start();
    }
}
class Data {
    public static synchronized void a() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println("A");
    }
    public synchronized void b(){
        System.out.println("B");
    }

    public void c(){
        System.out.println("C");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Last update: December 28, 2021 13:51
Contributors: PPG007