Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会有解决很多问题]生产者消费者模型
http://blog.csdn.net/a352193394/article/details/39503857
java 线程池 并行 执行 http://www.cnblogs.com/donaldlee2008/p/5290169.html


版权声明:本文为博主原创文章,未经博主允许不得转载。
在多线程中,两个线程之间交换数据是非常常见的情况,我们可以使用公共的数据结构,同样,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在线程之间交换数据[这个结合多线程并行会有解决很多问题]生产者消费者模型的更多相关文章
- JAVA多线程学习八-多个线程之间共享数据的方式
多个线程访问共享对象和数据的方式 如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 如果每个线程执行的代码不同,这 ...
- Android多线程研究(5)——线程之间共享数据
一.如果是每个线程都执行相同的代码,则可以使用同一个Runnable来实现共享 public class MultiThreadShareData { public static void main( ...
- Java生产者消费者模型
在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...
- java多线程:线程间通信——生产者消费者模型
一.背景 && 定义 多线程环境下,只要有并发问题,就要保证数据的安全性,一般指的是通过 synchronized 来进行同步. 另一个问题是,多个线程之间如何协作呢? 我们看一个仓库 ...
- Java并发工具类(四):线程间交换数据的Exchanger
简介 Exchanger(交换者)是一个用于线程间协作的工具类.Exchanger用于进行线程间的数据交换.它提供一个同步点,在这个同步点两个线程可以交换彼此的数据.这两个线程通过exchange方法 ...
- java 线程池、多线程实战(生产者消费者模型,1 vs 10) 附案例源码
导读 前二天写了一篇<Java 多线程并发编程>点我直达,放国庆,在家闲着没事,继续写剩下的东西,开干! 线程池 为什么要使用线程池 例如web服务器.数据库服务器.文件服务器或邮件服务器 ...
- java线程基础巩固---多线程下的生产者消费者模型,以及详细介绍notifyAll方法
在上一次[http://www.cnblogs.com/webor2006/p/8419565.html]中演示了多Product多Consumer假死的情况,这次解决假死的情况来实现一个真正的多线程 ...
- 第23章 java线程通信——生产者/消费者模型案例
第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...
- Java多线程15:Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型
Queue是什么 队列,是一种数据结构.除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的.无论使用哪种排序方式,队列的头都是调用remove()或poll()移 ...
随机推荐
- 简单封装常用js方法
1.uploadfiy插件封装 /* 参数:uploadID:上传控件ID url:请求后台url路径 callback:回调函数 */ uploadfiy({ uploadID: $('#btn ...
- Q & A
1 使用linux命令或者shell实现:文件words存放英文单词,格式为每行一个英文单词(单词可以重复),统计这个文件中出现次数最多的前10个单词. cat words.txt | sort | ...
- Openlayer 3 最简单的弹出框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- centos7下用yum安装mysql5.7
1.安装mysql源 下载地址:http://dev.mysql.com/downloads/repo/yum/ 下载之后用yum安装:yum localinstall -y xx.noarch.rp ...
- Android实现动画循环的方式
每次想到循环播放.重复执行时,脑海中总是冒出在while(true)的实现方式. Thread thread = new Thread(new Runnable(){ public void run( ...
- html音视频标签
音视频标签是html5标签,分别为<audio></audio>和<video></video>,这两个标签用法大致相同,且都仅在IE9及以上版本和其他 ...
- javascript keycode
keycode 8 = BackSpace BackSpacekeycode 9 = Tab Tabkeycode 12 = Clearkeycode 13 = Enterkeyc ...
- hbase checkandput
HBaseEveryDay_Atomic_compare_and_set Updated Sep 29, 2011 by lurker.h...@gmail.com 基本概念 在HBase中Inser ...
- @NotNull vs @Column(nullable = false)
参考:Confusion: @NotNull vs @Column(nullable = false)
- Spring Security(03)——核心类简介
目录 1.1 Authentication 1.2 SecurityContextHolder 1.3 AuthenticationManager和Authentication ...