Skip to content

Commit b341f25

Browse files
committed
exchanger commit
1 parent 23baad7 commit b341f25

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.concurrency.features.exchanger;
2+
3+
import com.example.concurrency.features.threadPool.ThreadPoolBuilder;
4+
5+
import java.util.concurrent.Exchanger;
6+
import java.util.concurrent.ThreadPoolExecutor;
7+
8+
/**
9+
* 描述:
10+
* 交换者 用于两个线程间的数据交换
11+
*
12+
* @author zed
13+
* @since 2019-07-03 5:39 PM
14+
*/
15+
public class ExchangerExample {
16+
private static final Exchanger<String> EXCHANGER = new Exchanger<>();
17+
private static ThreadPoolExecutor poolExecutor = ThreadPoolBuilder.fixedPool().setPoolSize(2).build();
18+
19+
public static void main(String[] args) {
20+
poolExecutor.execute(()->{
21+
try{
22+
String s ="SomethingAndA";
23+
EXCHANGER.exchange(s);
24+
}catch (InterruptedException e){
25+
e.printStackTrace();
26+
}
27+
});
28+
poolExecutor.execute(()->{
29+
try{
30+
String s1 = "SomethingAndB";
31+
String s = EXCHANGER.exchange("s1");
32+
System.out.println("s和s1值是否相等:"+s1.equals(s)+",s:"+s+",s1:"+s1);
33+
}catch (InterruptedException e){
34+
e.printStackTrace();
35+
}
36+
});
37+
poolExecutor.shutdown();
38+
}
39+
}
40+

0 commit comments

Comments
 (0)