如果两个线程在运行过程中需要交换彼此的信息,比如一个数据或者使用的空间,就需要用到Exchanger这个类,Exchanger为线程交换信息提供了非常方便的途径,它可以作为两个线程交换对象的同步点,只有当每个线程都在进入 exchange ()方法并给出对象时,才能接受其他线程返回时给出的对象。
      
       Exchanger的构造方法如下:
[java] 
Exchanger();  //创建一个新的 Exchanger。

Exchanger用到的主要方法有:
[java]
exchange(V x);  //等待另一个线程到达此交换点(除非它被中断),然后将给定的对象传送给该线程,并接收该线程的对象。 
exchange(V x, long timeout, TimeUnit unit);   // 等待另一个线程到达此交换点(除非它被中断,或者超出了指定的等待时间),然后将给定的对象传送给该线程,同时接收该线程的对象。

下面是demo代码:

 package com.xt.thinks21_7;

 import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit; public class ThreadLocalTest { public static void main(String[] args) {
Exchanger<List<Integer>> exchanger = new Exchanger<List<Integer>>();
new Thread1(exchanger).start();
new Thread2(exchanger).start();
} } class Thread1 extends Thread {
List<Integer> list = new ArrayList<Integer>();
Exchanger<List<Integer>> exchanger = null; public Thread1(Exchanger<List<Integer>> exchanger) {
this.exchanger = exchanger;
} @Override
public void run() {
Random rand = new Random();
list.add(rand.nextInt(10000));
list.add(rand.nextInt(10000));
list.add(rand.nextInt(10000));
list.add(rand.nextInt(10000));
list.add(rand.nextInt(10000));
System.out.println("\nThread1:list-->" + list.size() + "\n" + list);
for (int i = 0; i < 10; i++) {
try {
list = exchanger.exchange(list);
System.out.println("\nThread1:sizeoflist-->" + list.size()
+ "\n" + list);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} class Thread2 extends Thread {
List<Integer> list = new ArrayList<Integer>();
Exchanger<List<Integer>> exchanger = null; public Thread2(Exchanger<List<Integer>> exchanger) {
this.exchanger = exchanger;
} @Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Random rand = new Random();
list.add(rand.nextInt(10000));
list.add(rand.nextInt(10000));
list.add(rand.nextInt(10000));
list.add(rand.nextInt(10000));
list.add(rand.nextInt(10000));
System.out.println("\nThread2:list-->" + list.size() + "\n" + list);
for (int i = 0; i < 10; i++) {
try {
list = exchanger.exchange(list);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("\nThread2:sizeoflist-->" + list.size() + "\n"
+ list);
}
}
}

输出结果:

Thread1:list-->5

[5943, 3873, 5422, 2297, 3070]

Thread2:list-->5

[2774, 3676, 2137, 1446, 9944]

Thread2:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread1:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread2:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread1:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread1:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread2:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread2:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread1:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread2:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread1:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread1:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread2:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread2:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread1:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread1:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread2:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread2:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread1:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

Thread1:sizeoflist-->5

[5943, 3873, 5422, 2297, 3070]

Thread2:sizeoflist-->5

[2774, 3676, 2137, 1446, 9944]

从输出结果中可以看出两个线程互相交换数据,直到循环结束。

05-11 15:17