代码片段

锁对象

非静态同步方法的锁是 this

静态同步方法的锁是 类名.class

单例设计模式

饿汉式

1
2
3
4
5
6
7
class Single {
private static final Single s = new Single();
private Single() {}
public static Single getInstance() {
return s;
}
}

懒汉式

特点:延迟加载

1
2
3
4
5
6
7
8
9
10
class Single {
private static Single s = null;
private Single () {}
public static Single getInstance() {
if (s == null) {
s = new Single();
}
return s;
}
}

解决多进程安全问题:

1
2
3
4
5
6
7
8
9
10
class Single {
private static Single s = null;
private Single () {}
public static synchronized Single getInstance() {
if (s == null) {
s = new Single();
}
return s;
}
}

提高效率:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Single {
private static Single s = null;
private Single () {}
public static Single getInstance() {
if (s == null) {
synchronized(Single.class) {
if (s == null) {
s = new Single();
}
}
}
return s;
}
}

死锁

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
public class DeadLockDemo {
public static void main(String[] args) {
DeadLock d1 = new DeadLock(true);
DeadLock d2 = new DeadLock(false);
new Thread(d1).start();
new Thread(d2).start();
}
}

class DeadLock implements Runnable {
private boolean flag;

public DeadLock(boolean flag) {
this.flag = flag;
}

public void run() {
if (flag) {
synchronized (MyLock.myLockA) {
System.out.println("if myLockA");
synchronized (MyLock.myLockB) {
System.out.println("if myLockB");
}
}
} else {
synchronized (MyLock.myLockB) {
System.out.println("else myLockB");
synchronized (MyLock.myLockA) {
System.out.println("else myLockA");
}
}
}
}
}

class MyLock {
public static Object myLockA = new Object();
public static Object myLockB = new Object();
}

线程通信和等待/唤醒机制

学习版

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class ThreadInputOutputDemo {
public static void main(String[] args) {
Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);
new Thread(in).start();
new Thread(out).start();
}
}

class Res {
public String name;
public String sex;
public boolean flag = false;
}

class Input implements Runnable {
private Res r;

public Input(Res r) {
this.r = r;
}

public void run() {
boolean flag = true;
while (true) {
synchronized (Res.class) {
if (r.flag) {
try {
Res.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (flag) {
this.r.name = "Jerry";
this.r.sex = "Man";
flag = false;
} else {
this.r.name = "小红";
this.r.sex = "女";
flag = true;
}
r.flag = true;
Res.class.notify();
}
}
}
}

class Output implements Runnable {
private Res r;

public Output(Res r) {
this.r = r;
}

public void run() {
while (true) {
synchronized (Res.class) {
if (!r.flag) {
try {
Res.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(r.name + " ... " + r.sex);
r.flag = false;
Res.class.notify();
}
}
}
}

优化版

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class ThreadInputOutputDemo {
public static void main(String[] args) {
Res r = new Res();
Input in = new Input(r);
Output out = new Output(r);
new Thread(in).start();
new Thread(out).start();
}
}

class Res {
private String name;
private String sex;
private boolean flag = false;

public synchronized void set(String name, String sex) {
if (this.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
this.sex = sex;
this.flag = false;
this.notify();
}

public synchronized void out() {
if (!this.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.name + " ...... " + this.sex);
this.flag = false;
this.notify();
}
}

class Input implements Runnable {
private Res r;

public Input(Res r) {
this.r = r;
}

public void run() {
boolean flag = true;
while (true) {
if (flag) {
this.r.set("Jerry", "Man");
flag = false;
} else {
this.r.set("小红", "女");
flag = true;
}
}
}
}

class Output implements Runnable {
private Res r;

public Output(Res r) {
this.r = r;
}

public void run() {
while (true) {
this.r.out();
}
}
}

生产者消费者的 synchronized 方案

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public class ProducerConsumerDemo {
public static void main(String[] args) {
Resource res = new Resource("商品");
Producer pro1 = new Producer(res);
Consumer con1 = new Consumer(res);
Producer pro2 = new Producer(res);
Consumer con2 = new Consumer(res);

new Thread(pro1).start();
new Thread(con1).start();
new Thread(pro2).start();
new Thread(con2).start();
}
}

class Resource {
private String name;
private boolean flag = false;

public Resource(String name) {
this.name = name;
}

public synchronized void input(String name) {
while (this.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
System.out.println("生产者 ... " + this.name);
this.flag = true;
this.notifyAll();
}

public synchronized void out() {
while (!this.flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费者 ... " + this.name + " ... ");
this.flag = false;
this.notifyAll();
}
}

class Producer implements Runnable {
private Resource res;
private int count = 0;

public Producer(Resource res) {
this.res = res;
}

public void run() {
while (true) {
this.res.input("商品" + count++);
}
}
}

class Consumer implements Runnable {
private Resource res;

public Consumer(Resource res) {
this.res = res;
}

public void run() {
while (true) {
this.res.out();
}
}
}

生产者消费者的 lock 方案

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.concurrent.locks.*;

public class ProducerConsumerLockDemo {
public static void main(String[] args) {
Resource res = new Resource("商品");
Producer pro1 = new Producer(res);
Consumer con1 = new Consumer(res);
Producer pro2 = new Producer(res);
Consumer con2 = new Consumer(res);

new Thread(pro1).start();
new Thread(con1).start();
new Thread(pro2).start();
new Thread(con2).start();
}
}

class Resource {
private String name;
private boolean flag = false;
private Lock lock = new ReentrantLock();
private Condition inputCondition = lock.newCondition();
private Condition outCondition = lock.newCondition();

public Resource(String name) {
this.name = name;
}

public void input(String name) {
lock.lock();
try {
while (this.flag) {
try {
this.inputCondition.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
System.out.println("生产者 ... " + this.name);
this.flag = true;
this.outCondition.signalAll();
} finally {
lock.unlock();
}
}

public void out() {
lock.lock();
try {
while (!this.flag) {
try {
this.outCondition.await();
;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("消费者 ... " + this.name + " ... ");
this.flag = false;
this.inputCondition.signalAll();
} finally {
lock.unlock();
}
}
}

class Producer implements Runnable {
private Resource res;
private int count = 0;

public Producer(Resource res) {
this.res = res;
}

public void run() {
while (true) {
this.res.input("商品" + count++);
}
}
}

class Consumer implements Runnable {
private Resource res;

public Consumer(Resource res) {
this.res = res;
}

public void run() {
while (true) {
this.res.out();
}
}
}