Javaのマルチスレッド - 交互スレッド

要件:
同期メカニズムによって、SLEEP()メソッド、アニメーション表示を実現するための参加()メソッドは、
スレッド:1,3,5,7,9
酢酸スレッド:2,4,6,8,10
プロパンスレッド:A、B 、C、D、Eの
メイン()スレッド出力:スレッドを開始し、糸端

出力:スレッドの開始、1-2 ## 3-B-4 ## 5-C-6 ## ...

考える:
複数の判定マーク、アナログ(消費者-プロデューサー)を使用して待機した後、スレッド当たりの出力を、そして彼らのマークアップ変える
重要なリソースを-複数を使用する== putXは()==メソッドは、自分のマークを決定するために( == isEmptyX ==)を出力する
実行スレッドの複数の交互に順序付けられた
コード:

リソース{クラス
真のプライベートisEmpty01 =ブール;
プライベートisEmpty02 =ブール値をfalseに、
プライベートisEmpty03 =ブール値をfalseに;

//置く方法待ちに一つの出力、各出力に対応する各、他の人を待つには、ウェイクアップ
、公共ボイドput1(){
しばらく( !isEmpty01){
試み{
待機();
}キャッチ(InterruptedExceptionあるE){
; e.printStackTrace()
}
}
を出力した後//
falseにisEmpty01 =;
isEmpty02 = trueに;
のnotifyAll();
}
公共ボイドput2(){
ながら、( !isEmpty02){
試み{
待機();
}キャッチ(InterruptedExceptionあるE){
e.printStackTrace();
}
}
isEmpty02 = falseに;
isEmpty03 = trueに;
のnotifyAll();
}
公共ボイドput3(){
(isEmpty03!){ながら
{みてください
)(待ちます。
}キャッチ(InterruptedExceptionある電子){
e.printStackTrace();
}
}
isEmpty03 = FALSE;
isEmpty01 =はtrue。
notifyAll();
}
}

クラスPlayer01はRunnableを実装して、{

プライベートリソースRESを、
プライベートString []型ARR。
Player01(){}
Player01(String []型ARR、リソースRES){
this.arr = ARR。
this.resの= resを。
}

公共ボイドラン(){
同期(RES){
ため(; iは<arr.length; I = 0 int型私は++){
//错误的点
// 61,62、这两句不能交换顺序
res.put1() ;
System.out.print([I] + ARR " - ")。
{試す
のThread.sleep(1000);
}キャッチ(InterruptedExceptionある電子){
e.printStackTrace();
}
}
}
}
}

クラスPlayer02はRunnableを実装して、{

プライベートリソースRESを、
プライベートString []型ARR。
Player02(){}
Player02(String []型ARR、リソースRES){
this.arr = ARR。
this.resの= resを。
}

公共ボイドラン(){
同期(RES){
{ため(iは++; iは<arr.length I = 0 INT)
)(res.put2 します。
System.out.print([I] + ARR " - ")。
{試す
のThread.sleep(1000);
}キャッチ(InterruptedExceptionある電子){
e.printStackTrace();
}
}
}
}
}

クラスPlayer03はRunnableを実装して、{

プライベートリソースRESを、
プライベートString []型ARR。
Player03(){}
Player03(String []型ARR、リソースRES){
this.arr = ARR。
this.resの= resを。
}

公共ボイドラン(){
同期(RES){
{ため(iは++; iは<arr.length I = 0 INT)
)(res.put3 します。
System.out.print(ARR [I] + "##")。
{試す
のThread.sleep(1000);
}キャッチ(InterruptedExceptionある電子){
e.printStackTrace();
}
}
}
}
}

クラスTest08 {

パブリック静的無効メイン(文字列[] args){

文字列[] ARR1 = { "1"、 "3"、 "5"、 "7"、 "9"}。
文字列[] ARR2 = { "A"、 "B"、 "C"、 "D"、 "E"}。
文字列[] ARR3 = { "2"、 "4"、 "6"、 "8"、 "0"}。

リソースRES =新しいリソース();

Player01 P1 =新しいPlayer01(ARR1、RES);
Player02 P2 =新しいPlayer02(ARR2、RES);
Player03 P3 =新しいPlayer03(ARR3、RES);

スレッドT1 =新しいスレッド(P1);
スレッドT2 =新しいスレッド(P2)。
スレッドT3 =新しいスレッド(P3)。

t1.start();
t2.start();
t3.start();
















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は、
132
133
134
135
136
137
138
139
140
141は、
142の
143
144
145
結果:


重要:
このマーカーの使用は、交互に注文した2つの実行スレッド上で実現することができます
---------------------

おすすめ

転載: www.cnblogs.com/ly570/p/10930949.html