《Java源码分析》:Exchanger

来源:互联网 发布:淘宝买药怎么付款 编辑:程序博客网 时间:2024/05/01 03:22

《Java源码分析》:Exchanger

Exchanger类用于两个线程之间交换数据。

Exchanger类只有两个方法:

1、V exchange(V x) 
等待另一个线程到达此交换点(除非当前线程被中断),然后将给定的对象传送给该线程,并接收该线程的对象。

API给出的详细说明如下:

public V exchange(V x) 
throws InterruptedException等待另一个线程到达此交换点(除非当前线程被中断),然后将给定的对象传送给该线程,并接收该线程的对象。

如果另一个线程已经在交换点等待,则出于线程调度目的,继续执行此线程,并接收当前线程传入的对象。当前线程立即返回,接收其他线程传递的交换对象。

如果还没有其他线程在交换点等待,则出于调度目的,禁用当前线程,且在发生以下两种情况之一前,该线程将一直处于休眠状态:

其他某个线程进入交换点;或者 
其他某个线程中断当前线程。 
如果当前线程:

在进入此方法时已经设置了该线程的中断状态;或者 
在等待交换时被中断, 
则抛出 InterruptedException,并且清除当前线程的已中断状态。

参数:

2、V exchange(V x, long timeout, TimeUnit unit) 
等待另一个线程到达此交换点(除非当前线程被中断,或者超出了指定的等待时间),然后将给定的对象传送给该线程,同时接收该线程的对象。

看Exchanger的应用例子如下:

    package com.wrh.readwritelock;    import java.util.concurrent.Exchanger;    public class ExchangerDemo {        private static Exchanger<String> exchanger = new Exchanger<String>();        public static void main(String[] args) {            new Thread(new Runnable(){                @Override                public void run() {                    for(int i=0;i<2;i++){                        String str = Integer.toString(i);                        System.out.println(Thread.currentThread().getName()+"交换前的数据为:"+str);                        String exchangeRes = null;                        try {                            exchangeRes = exchanger.exchange(str);                            System.out.println(Thread.currentThread().getName()                                    +"交换的数据情况为:从"+str+"----->"+exchangeRes);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                }            }).start();            new Thread(new Runnable(){                @Override                public void run() {                    int start = 10;                    int end = 12;                    for(int i=start;i<end;i++){                        String str = Integer.toString(i);                        System.out.println(Thread.currentThread().getName()+"交换前的数据为:"+str);                        String exchangeRes = null;                        try {                            exchangeRes = exchanger.exchange(str);                            System.out.println(Thread.currentThread().getName()                                    +"交换的数据情况为:从"+str+"----->"+exchangeRes);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                }            }).start();;        }    }
  • 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

运行结果为:

Thread-0交换前的数据为:0Thread-1交换前的数据为:10Thread-1交换的数据情况为:从10----->0Thread-0交换的数据情况为:从0----->10Thread-0交换前的数据为:1Thread-1交换前的数据为:11Thread-1交换的数据情况为:从11----->1Thread-0交换的数据情况为:从1----->11

小结

Exchanger用法很简单哈,需要我们记住的是:此类是用于两个线程交换数据的。