Java multi-thread - the thread alternately

Requirements:
by synchronization mechanism, SLEEP () method, the Join () method to realize animation display;
A thread: 1,3,5,7,9
acetate threads: 2,4,6,8,10
propan thread: a, b , C, D, E
main () thread output: start thread, the thread end

Output: the thread starting, 1-a-2 ## 3-b-4 ## 5-c-6 ## ...

Thinking:
using multiple decision marks, analog (consumer - producer) output per thread after a wait, and then change their markup
critical resource - use multiple == putX () == method, to determine their own mark ( == isEmptyX ==) and outputs
a plurality of execution threads alternately ordered
codes:

the Resource {class
Private isEmpty01 = Boolean to true;
Private isEmpty02 = Boolean to false;
Private isEmpty03 = Boolean to false;

// put methods each corresponding to one output, each output to a wait, wait for others wakeup
public void put1 () {
the while ( ! isEmpty01) {
the try {
the wait ();
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
// after output
isEmpty01 = to false;
isEmpty02 = to true;
notifyAll ();
}
public void put2 () {
the while ( ! isEmpty02) {
the try {
the wait ();
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
isEmpty02 = to false;
isEmpty03 = to true;
notifyAll ();
}
public void put3(){
while(!isEmpty03){
try{
wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
isEmpty03 = false;
isEmpty01 = true;
notifyAll();
}
}

class Player01 implements Runnable{

private Resource res;
private String[] arr;
Player01(){}
Player01(String[] arr,Resource res){
this.arr = arr;
this.res = res;
}

public void run(){
synchronized(res){
for(int i=0;i<arr.length;i++){
//错误的点
//61,62,这两句不能交换顺序
res.put1();
System.out.print(arr[i]+"-");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}

class Player02 implements Runnable{

private Resource res;
private String[] arr;
Player02(){}
Player02(String[] arr,Resource res){
this.arr = arr;
this.res = res;
}

public void run(){
synchronized(res){
for(int i=0;i<arr.length;i++){
res.put2();
System.out.print(arr[i]+"-");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}

class Player03 implements Runnable{

private Resource res;
private String[] arr;
Player03(){}
Player03(String[] arr,Resource res){
this.arr = arr;
this.res = res;
}

public void run(){
synchronized(res){
for(int i=0;i<arr.length;i++){
res.put3();
System.out.print(arr[i]+"## ");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}

class Test08{

public static void main(String[] args){

String[] arr1 = {"1","3","5","7","9"};
String[] arr2 = {"a","b","c","d","e"};
String[] arr3 = {"2","4","6","8","0"};

Resource res = new Resource();

Player01 p1 = new Player01(arr1,res);
Player02 p2 = new Player02(arr2,res);
Player03 p3 = new Player03(arr3,res);

Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
Thread t3 = new Thread(p3);

t1.start();
t2.start();
t3.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130.
131 is
132
133
134
135
136
137
138
139
140
141 is
142
143
144
145
The results:


Important:
This use of markers can be achieved over two threads of execution ordered alternately
---------------------

Guess you like

Origin www.cnblogs.com/ly570/p/10930949.html