码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2014-09-23 23:21:55      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:style   io   使用   java   ar   for   数据   sp   art   

在多线程中,两个线程之间交换数据是非常常见的情况,我们可以使用公共的数据结构,同样,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.


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

标签:style   io   使用   java   ar   for   数据   sp   art   

原文地址:http://blog.csdn.net/a352193394/article/details/39503857

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!