Synchronized的一些思考

Synchronized的一些思考。

前些天被问到Synchronized关键字的一些用法和区别,发现自己的理解比较浅,因此在这作一个总结。

0x00 定义

synchronized是一种同步锁。采用synchronized修饰符实现的同步机制叫做互斥锁机制,它所获得的锁叫做互斥锁。每个对象都有一个monitor(锁标记),当线程拥有这个锁标记时才能访问这个资源,没有锁标记便进入锁池。任何一个对象系统都会为其创建一个互斥锁,这个锁是为了分配给线程的,防止打断原子操作。每个对象的锁只能分配给一个线程,因此叫做互斥锁。

0x01 使用场景

按照作用域的划分,可以分为以下四种。

  1. 修饰代码块,作用范围为{}之间的代码,作用的对象是调用这个代码块的对象。
  2. 修饰普通方法,作用范围为整个方法,作用的对象为调用该方法的对象实例。
  3. 修饰静态方法,作用范围为整个类,作用的对象为该类的所有对象实例。
  4. 修饰类,如synchronized(xxx.class){},作用的范围为{}部分,作用的对象为该类的所有对象实例。

0x11 修饰代码块

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.kkk.pattern;

/**
* Created by z3jjlzt on 2018/1/23.
*/

public class SyncTest {
private void pNum(int num) {
synchronized(this){
System.out.println(Thread.currentThread());
for (int i = 0; i < 10; i++) {
System.out.print("^"+ i + "*");
}
System.out.println();
}
}
static class r implements Runnable {
SyncTest syncTest;
int num;

public r(SyncTest syncTest, int num) {
this.syncTest = syncTest;
this.num = num;
}

@Override
public void run() {
while (true) {
this.syncTest.pNum(this.num);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
SyncTest syncTest = new SyncTest();
Thread thread = new Thread(new r(syncTest, 1));
Thread thread1 = new Thread(new r(syncTest, 1));
thread.start();
thread1.start();
}
}
输出结果:
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
...

通过输出可以看出,当多个线程同时调用一个synchronized方法时,在同一时间,只能有一个线程能够持有该方法的锁并执行该方法,其他线程只能等待。
需要注意的时,在两个线程中,传入的时同一个对象SyncTest,如果传入的对象不相同,那么两个线程的代码可以并发执行,原因是synchronized只锁定对象实例,不同对象实例之间的锁互不影响

0x11 修饰普通方法

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.kkk.pattern;

/**
* Created by z3jjlzt on 2018/1/23.
*/

public class SyncTest {
private synchronized void pNum(int num) {
System.out.println(Thread.currentThread());
for (int i = 0; i < 10; i++) {
System.out.print("^"+ i + "*");
}
System.out.println();
}
static class r implements Runnable {
SyncTest syncTest;
int num;

public r(SyncTest syncTest, int num) {
this.syncTest = syncTest;
this.num = num;
}

@Override
public void run() {
while (true) {
this.syncTest.pNum(this.num);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
SyncTest syncTest = new SyncTest();
Thread thread = new Thread(new r(syncTest, 1));
Thread thread1 = new Thread(new r(syncTest, 1));
thread.start();
thread1.start();
}
}
输出结果:
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
...

输出结果和前面相同,但由于synchronized的系统开销比较大,所以在都能实现功能的前提下,尽量多使用同步代码块的方式来减少系统开销。

0x100 修饰静态方法

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.kkk.pattern;

/**
* Created by z3jjlzt on 2018/1/23.
*/

public class SyncTest {
private static synchronized void pNum(int num) {
System.out.println(Thread.currentThread());
for (int i = 0; i < 10; i++) {
System.out.print("^"+ i + "*");
}
System.out.println();
}
static class r implements Runnable {
SyncTest syncTest;
int num;

public r(SyncTest syncTest, int num) {
this.syncTest = syncTest;
this.num = num;
}

@Override
public void run() {
while (true) {
this.syncTest.pNum(this.num);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
SyncTest syncTest = new SyncTest();
SyncTest syncTest1 = new SyncTest();
Thread thread = new Thread(new r(syncTest, 1));
Thread thread1 = new Thread(new r(syncTest1, 1));
thread.start();
thread1.start();
}
}
输出结果:
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
...

在该情况下,synchronized作用的对象是类的所有实例对象。

0x101 修饰一个类

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.kkk.pattern;

/**
* Created by z3jjlzt on 2018/1/23.
*/

public class SyncTest {
private void pNum(int num) {
synchronized(this){
System.out.println(Thread.currentThread());
for (int i = 0; i < 10; i++) {
System.out.print("^"+ i + "*");
}
System.out.println();
}
}
static class r implements Runnable {
SyncTest syncTest;
int num;

public r(SyncTest syncTest, int num) {
this.syncTest = syncTest;
this.num = num;
}

@Override
public void run() {
while (true) {
this.syncTest.pNum(this.num);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

public static void main(String[] args) {
SyncTest syncTest = new SyncTest();
SyncTest syncTest1 = new SyncTest();
Thread thread = new Thread(new r(syncTest, 1));
Thread thread1 = new Thread(new r(syncTest1, 1));
thread.start();
thread1.start();
}
}
输出结果:
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-0,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
Thread[Thread-1,5,main]
^0*^1*^2*^3*^4*^5*^6*^7*^8*^9*
...

输出结果和修饰静态方法是一致的。

0x110 一些补充

  • 在同一个对象实例中,有多个synchronized修饰的普通方法,多个线程同时访问这些方法(同一个对象实例中),在同一时间内,只能有一个线程能够取得该对象的锁并执行程序,因为在同步方法中,只有获取了锁才能执行方法,而一个对象只有一个锁
  • 当只是想让一段代码同步时,可以创建一个特殊的对象来充当锁,生成零长度的byte[]对象只需3条操作码,而Object lock = new Object()则需要7行操作码。

0xff 参考