Two ways of Sequence Printer implementation

题目:两个线程,分别打印[1,3,5]和[2,4,6],写一个程序,打印[1,2,3,4,5,6]。
下面列出两种解法,分别用同步代码块和锁,具体参见程序。

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
96
package com.freelemon.concurrency.threads;
public class SeqPrinter1 {
private Object obj = new Object();
private boolean isThread1 = true;
public static void main(String[] args){
SeqPrinter1 printer1 = new SeqPrinter1();
printer1.printSequence();
}
public void printSequence() {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (obj){
System.out.println(1);
isThread1 = false;
obj.notify();
while (!isThread1){
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(3);
isThread1 = false;
obj.notify();
while (!isThread1){
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(5);
isThread1 = false;
obj.notify();
}
}
}, "Thread-1").start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (obj){
while (isThread1){
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(2);
isThread1 = true;
obj.notify();
while (isThread1){
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(4);
isThread1 = true;
obj.notify();
while (isThread1){
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(6);
isThread1 = true;
obj.notify();
}
}
}, "Thread-2").start();
}
}
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
package com.freelemon.concurrency.threads;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SeqPrinter2 {
private Lock lock = new ReentrantLock();
private Condition thread1Ready = lock.newCondition();
private Condition thread2Ready = lock.newCondition();
public static void main(String[] args){
SeqPrinter2 printer2 = new SeqPrinter2();
printer2.printSequence();
}
public void printSequence(){
new Thread(new Runnable() {
@Override
public void run() {
try{
lock.lock();
thread1Ready.await();
System.out.println(1);
thread2Ready.signal();
thread1Ready.await();
System.out.println(3);
thread2Ready.signal();
thread1Ready.await();
System.out.println(5);
thread2Ready.signal();
thread1Ready.await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}, "Thread-1").start();
new Thread(new Runnable() {
@Override
public void run() {
try{
lock.lock();
thread1Ready.signal();
thread2Ready.await();
System.out.println(2);
thread1Ready.signal();
thread2Ready.await();
System.out.println(4);
thread1Ready.signal();
thread2Ready.await();
System.out.println(6);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}, "Thread-2").start();
}
}