《Java源码分析》:Exchanger

《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交换前的数据为:0
Thread-1交换前的数据为:10
Thread-1交换的数据情况为:从10----->0
Thread-0交换的数据情况为:从0----->10
Thread-0交换前的数据为:1
Thread-1交换前的数据为:11
Thread-1交换的数据情况为:从11----->1
Thread-0交换的数据情况为:从1----->11

小结

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值