Java多线程之~~~使用Exchanger在线程之间交换数据

在多线程中,两个线程之间交换数据是非常常见的情况,我们可以使用公共的数据结构,同样,Java也提供了很好

的类供我们使用,那就是Exchanger类,这个类可以帮助我们在两个线程之间同步数据结构,下面我们以这个类再来实

现一遍生产者消费者模型,貌似这个模型已经被写烂了。


package com.bird.concursey.charpet5;

import java.util.List;
import java.util.concurrent.Exchanger;

public class Producer implements Runnable {
	//This will be the data structure that the producer will interchange with the consumer.
	private List<String> buffer;
	
	private Exchanger<List<String>> exchanger;
	
	public Producer(List<String> buffer, Exchanger<List<String>> exchanger) {
		super();
		this.buffer = buffer;
		this.exchanger = exchanger;
	}

	@Override
	public void run() {
		int cycle = 1;
		for(int i = 0; i < 10; i++) {
			System.out.printf("Producer: Cycle %d\n",cycle);
			for (int j=0; j<10; j++){
				String message="Event "+((i*10)+j);
				System.out.printf("Producer: %s\n",message);
				buffer.add(message);
			}
			try {
				buffer = exchanger.exchange(buffer);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("Producer: "+buffer.size());
			cycle++;
		}
	}

}

package com.bird.concursey.charpet5;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Exchanger;

public class Consumer implements Runnable {
	
	private List<String> buffer;
	
	private Exchanger<List<String>> exchange;
	
	public Consumer(List<String> buffer, Exchanger<List<String>> exchange) {
		super();
		this.buffer = buffer;
		this.exchange = exchange;
	}


	@Override
	public void run() {
		int cycle = 1;
		for(int i = 0; i < 10; i++) {
			System.out.printf("Consumer: Cycle %d\n",cycle);
			try {
				buffer = exchange.exchange(buffer);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("Consumer: "+buffer.size());
			for (int j=0; j<10; j++){
				String message=buffer.get(0);
				System.out.println("Consumer: "+message);
				buffer.remove(0);
			}
			cycle++;
		}
	}
	
	public static void main(String[] args) {
		List<String> buffer1 = new ArrayList<String>();
		List<String> buffer2 = new ArrayList<String>();
		
		Exchanger<List<String>> exchange = new Exchanger<List<String>>();
		
		Producer producer = new Producer(buffer1, exchange);
		Consumer consumer = new Consumer(buffer2, exchange);
		
		Thread threadProducer=new Thread(producer);
		Thread threadConsumer=new Thread(consumer);
		
		threadProducer.start();
		threadConsumer.start();
	}

}

The consumer begins with an empty buffer and calls Exchanger to synchronize with the
producer. It needs data to consume. The producer begins its execution with an empty buffer.
It creates 10 strings, stores it in the buffer, and uses the exchanger to synchronize with
the consumer.

At this point, both threads (producer and consumer) are in Exchanger and it changes the
data structures, so when the consumer returns from the exchange() method, it will have a
buffer with 10 strings. When the producer returns from the exchange() method, it will have
an empty buffer to fill again. This operation will be repeated 10 times.

If you execute the example, you will see how producer and consumer do their jobs
concurrently and how the two objects interchange their buffers in every step. As it occurs with
other synchronization utilities, the first thread that calls the exchange() method was put to
sleep until the other threads arrived.


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值