forked from fengshao0907/JavaCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducer.java
More file actions
56 lines (49 loc) · 1.28 KB
/
Copy pathProducer.java
File metadata and controls
56 lines (49 loc) · 1.28 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
package com.javaCore.concurrency.Exchanger;
import java.util.List;
import java.util.concurrent.Exchanger;
/**
* �����
* @Project:javaConcurrency
* @file:Producer.java
*
* @Author:chenssy
* @email:[email protected]
* @url : http://cmsblogs.com
* @qq : 122448894
*
* @data:2015��9��21��
*/
public class Producer implements Runnable{
/**
* ����ߺ�����߽��н�������ݽṹ
*/
private List<String> buffer;
/**
* ͬ������ߺ�����ߵĽ�������
*/
private final Exchanger<List<String>> exchanger;
Producer(List<String> buffer,Exchanger<List<String>> exchanger){
this.buffer = buffer;
this.exchanger = exchanger;
}
@Override
public void run() {
int cycle = 1;
for(int i = 0 ; i < 10 ; i++){
System.out.println("Producer : Cycle :" + cycle);
for(int j = 0 ; j < 10 ; j++){
String message = "Event " + ((i * 10 ) + j);
System.out.println("Producer : " + message);
buffer.add(message);
}
//����exchange()������߽�����ݽ���
try {
buffer = exchanger.exchange(buffer);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Producer :" + buffer.size());
cycle++ ;
}
}
}