標籤:
如果兩個線程在運行過程中需要交換彼此的資訊,比如一個資料或者使用的空間,就需要用到Exchanger這個類,Exchanger為線程交換資訊提供了非常方便的途徑,它可以作為兩個線程交換對象的同步點,只有當每個線程都在進入 exchange ()方法並給出對象時,才能接受其他線程返回時給出的對象。
Exchanger的構造方法如下:
[java]
Exchanger(); //建立一個新的 Exchanger。
Exchanger用到的主要方法有:
[java]
exchange(V x); //等待另一個線程到達此交換點(除非它被中斷),然後將給定的對象傳送給該線程,並接收該線程的對象。
exchange(V x, long timeout, TimeUnit unit); // 等待另一個線程到達此交換點(除非它被中斷,或者超出了指定的等待時間),然後將給定的對象傳送給該線程,同時接收該線程的對象。
下面是demo代碼:
1 package com.xt.thinks21_7; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Random; 6 import java.util.concurrent.Exchanger; 7 import java.util.concurrent.TimeUnit; 8 9 public class ThreadLocalTest {10 11 public static void main(String[] args) {12 Exchanger<List<Integer>> exchanger = new Exchanger<List<Integer>>();13 new Thread1(exchanger).start();14 new Thread2(exchanger).start();15 }16 17 }18 19 class Thread1 extends Thread {20 List<Integer> list = new ArrayList<Integer>();21 Exchanger<List<Integer>> exchanger = null;22 23 public Thread1(Exchanger<List<Integer>> exchanger) {24 this.exchanger = exchanger;25 }26 27 @Override28 public void run() {29 Random rand = new Random();30 list.add(rand.nextInt(10000));31 list.add(rand.nextInt(10000));32 list.add(rand.nextInt(10000));33 list.add(rand.nextInt(10000));34 list.add(rand.nextInt(10000));35 System.out.println("\nThread1:list-->" + list.size() + "\n" + list);36 for (int i = 0; i < 10; i++) {37 try {38 list = exchanger.exchange(list);39 System.out.println("\nThread1:sizeoflist-->" + list.size()40 + "\n" + list);41 } catch (InterruptedException e) {42 // TODO Auto-generated catch block43 e.printStackTrace();44 }45 }46 }47 }48 49 class Thread2 extends Thread {50 List<Integer> list = new ArrayList<Integer>();51 Exchanger<List<Integer>> exchanger = null;52 53 public Thread2(Exchanger<List<Integer>> exchanger) {54 this.exchanger = exchanger;55 }56 57 @Override58 public void run() {59 try {60 TimeUnit.MILLISECONDS.sleep(10);61 } catch (InterruptedException e1) {62 // TODO Auto-generated catch block63 e1.printStackTrace();64 }65 Random rand = new Random();66 list.add(rand.nextInt(10000));67 list.add(rand.nextInt(10000));68 list.add(rand.nextInt(10000));69 list.add(rand.nextInt(10000));70 list.add(rand.nextInt(10000));71 System.out.println("\nThread2:list-->" + list.size() + "\n" + list);72 for (int i = 0; i < 10; i++) {73 try {74 list = exchanger.exchange(list);75 } catch (InterruptedException e) {76 // TODO Auto-generated catch block77 e.printStackTrace();78 }79 System.out.println("\nThread2:sizeoflist-->" + list.size() + "\n"80 + list);81 }82 }83 }
輸出結果:
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]
從輸出結果中可以看出兩個線程互相交換資料,直到迴圈結束。
JAVA之Exchanger