File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
src/main/java/com/example/concurrency/features/exchanger Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments