-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
339 lines (325 loc) · 8.5 KB
/
Copy pathMain.java
File metadata and controls
339 lines (325 loc) · 8.5 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//class myThread implements Runnable{
// private int tickets = 10;
//
//// @Override
//// public void run(){
//// for(int i=0;i<1000;i++){
//// //同步代码块
//// synchronized (this){
//// if(this.tickets > 0){
//// try{
//// Thread.sleep(200);
//// }
//// catch (Exception e){
//// e.printStackTrace();
//// }
//// System.out.println(Thread.currentThread().getName()+"还剩"+tickets--+"票");
//// }
//// }
//// }
//// }
// public void run(){
// for(int i=0; i<1000;i++){
// this.sale();
// }
// }
//
// private synchronized void sale(){
// if(this.tickets > 0){
// try {
// Thread.sleep(200);
// }catch (Exception e){
// e.printStackTrace();
// }
// System.out.println(Thread.currentThread().getName()+"还剩"+tickets--+"票");
// }
// }
//}
//
//public class Main {
// public static void main(String[] args){
// myThread mt = new myThread();
// new Thread(mt,"threadA").start();
// new Thread(mt,"threadB").start();
// }
//}
//class Sync {
// private String word = "Hello, Java";
// public void test() {
// synchronized (this) {
// System.out.println("test begin ..." + Thread.currentThread().getName() + " " + this.word);
// try {
// Thread.sleep(200);
// } catch (Exception e) {
// e.printStackTrace();
// }
// System.out.println("test end ..." + Thread.currentThread().getName());
// }
// }
//}
//class myThread implements Runnable {
// private Sync sync;
// public myThread(Sync sync){
// this.sync = sync;
// }
//
// public void run(){
// this.sync.test();
// }
//}
//
//public class Main {
// public static void main(String[] args){
// Sync sync = new Sync();
//
// for(int i=0;i<3 ;i++){
// myThread mt = new myThread(sync);
// new Thread(mt,"thread"+i).start();
// }
// }
//}
//使用全局锁
//class Sync {
// public void test(){
// synchronized (Sync.class){
// System.out.println("test begin ..." + Thread.currentThread().getName());
// try {
// Thread.sleep(200);
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// System.out.println("test end ..." + Thread.currentThread().getName());
// }
// }
//}
//
//class myThread implements Runnable {
// public void run(){
// new Sync().test();
// }
//}
//
//public class Main {
// public static void main(String[] args){
// myThread mt = new myThread();
// for(int i=0;i<10;i++){
// new Thread(mt,"thread "+i).start();
// }
// }
//}
//import java.util.concurrent.SynchronousQueue;
//
//class Pen {
// private String pen = "this is a pen";
//
// public String getPen() {
// return pen;
// }
//}
//
//class Book {
// private String book = "this is a book";
//
// public String getBook(){
// return book;
// }
//}
//
//public class Main{
// private static Pen pencil = new Pen();
// private static Book newBook = new Book();
//
// public void deadLock() {
// Thread thread1 = new Thread(new Runnable(){
// @Override
// public void run() {
// synchronized (pencil){
// System.out.println(Thread.currentThread().getName() + " 我有笔,我就不给你");
//
// synchronized (newBook){
// System.out.println(Thread.currentThread().getName()+ " 把你的书给我");
// }
// }
// }
// },"pen");
//
// Thread thread2 = new Thread(new Runnable() {
// @Override
// public void run() {
// synchronized (newBook){
// System.out.println(Thread.currentThread().getName()+"我有书,我就不给你");
//
// synchronized (pencil){
// System.out.println(Thread.currentThread().getName()+"把你的笔借我用用");
// }
// }
// }
// },"book");
// thread1.start();
// thread2.start();
// }
// public static void main(String[] args) {
// new Main().deadLock();
// }
//}
//生产者消费者模型(不带条件变量和锁资源)
//class Data {
// private String title ;
// private String note ;
//
// public void setTitle(String title){
// this.title = title ;
// }
//
// public String getTitle() {
// return title ;
// }
//
// public void setNote(String note){
// this.note = note ;
// }
//
// public String getNote() {
// return note;
// }
//}
//
//class DataConsumer implements Runnable {
// private Data data;
//
// public DataConsumer(Data data) {
// super();
// this.data = data;
// }
//
// @Override
// public void run(){
// for(int i=0; i<50; i++){
// try{
// Thread.sleep(500);
// }catch (InterruptedException e){
// e.printStackTrace();
// }
// System.out.println(this.data.getTitle()+"##"+this.data.getNote());
// }
// }
//}
//
//class DataProvider implements Runnable {
// private Data data;
// public DataProvider(Data data){
// super();
// this.data = data ;
// }
//
// @Override
// public void run() {
// for(int i=0; i<50; i++){
// try{
// Thread.sleep(500);
// }catch (Exception e){
// e.printStackTrace();
// }
// if(i%2 == 0){
// this.data.setTitle("lebron-james");
// this.data.setNote("联盟第一人");
// }else{
// this.data.setTitle("kevin-durant");
// this.data.setNote("千年老二");
// }
// }
// }
//}
//
//public class Main {
// public static void main(String[] args) {
// Data data = new Data();
// new Thread(new DataConsumer(data)).start();
// new Thread(new DataProvider(data)).start();
// }
//}
class Data {
private String title ;
private String note ;
private boolean flag ;
//flag = true :表示允许生产,不允许消费
//flag = false :表示生产完毕,可以消费
public synchronized void set(String title, String node){
if(this.flag == false){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
this.title = title ;
try {
Thread.sleep(10);
}catch (InterruptedException e){
e.printStackTrace();
}
this.note = note;
this.flag = false ;
super.notify();
}
public synchronized void get(){
if(this.flag == true ){
try{
super.wait();
}catch(InterruptedException e){
e.printStackTrace();
}
}
try{
Thread.sleep(20);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(this.title + "###"+this.note);
this.flag = true;
super.notify();
}
}
class DataProvider implements Runnable {
private Data data;
public DataProvider(Data data){
super();
this.data = data ;
}
@Override
public void run(){
for(int i=0;i<20;i++){
if(i%2 == 0){
this.data.set("lebron-james","联盟第一人");
}else{
this.data.set("kevin-durant","千年老二");
}
}
}
}
class DataConsumer implements Runnable {
private Data data;
public DataConsumer(Data data){
super();
this.data = data;
}
@Override
public void run(){
for(int i=0; i<50; i++){
try{
Thread.sleep(500);
}catch (InterruptedException e){
e.printStackTrace();
}
this.data.get();
}
}
}
public class Main {
public static void main(String[] args) {
Data data = new Data();
new Thread(new DataProvider(data)).start();
new Thread(new DataConsumer(data)).start();
}
}