http://blog.csdn.net/a352193394/article/details/39503857

 Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会有解决很多问题]
具体看 http://www.cnblogs.com/donaldlee2008/p/5290169.html

java 线程池 并行 执行   http://www.cnblogs.com/donaldlee2008/p/5290169.html

2014-09-23 20:48 1205人阅读 评论(0) 收藏 举报
 分类:
多线程(22) 

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

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

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

  1. package com.bird.concursey.charpet5;
  2. import java.util.List;
  3. import java.util.concurrent.Exchanger;
  4. public class Producer implements Runnable {
  5. //This will be the data structure that the producer will interchange with the consumer.
  6. private List<String> buffer;
  7. private Exchanger<List<String>> exchanger;
  8. public Producer(List<String> buffer, Exchanger<List<String>> exchanger) {
  9. super();
  10. this.buffer = buffer;
  11. this.exchanger = exchanger;
  12. }
  13. @Override
  14. public void run() {
  15. int cycle = 1;
  16. for(int i = 0; i < 10; i++) {
  17. System.out.printf("Producer: Cycle %d\n",cycle);
  18. for (int j=0; j<10; j++){
  19. String message="Event "+((i*10)+j);
  20. System.out.printf("Producer: %s\n",message);
  21. buffer.add(message);
  22. }
  23. try {
  24. buffer = exchanger.exchange(buffer);
  25. } catch (InterruptedException e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. }
  29. System.out.println("Producer: "+buffer.size());
  30. cycle++;
  31. }
  32. }
  33. }
  1. package com.bird.concursey.charpet5;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.concurrent.Exchanger;
  5. public class Consumer implements Runnable {
  6. private List<String> buffer;
  7. private Exchanger<List<String>> exchange;
  8. public Consumer(List<String> buffer, Exchanger<List<String>> exchange) {
  9. super();
  10. this.buffer = buffer;
  11. this.exchange = exchange;
  12. }
  13. @Override
  14. public void run() {
  15. int cycle = 1;
  16. for(int i = 0; i < 10; i++) {
  17. System.out.printf("Consumer: Cycle %d\n",cycle);
  18. try {
  19. buffer = exchange.exchange(buffer);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. System.out.println("Consumer: "+buffer.size());
  24. for (int j=0; j<10; j++){
  25. String message=buffer.get(0);
  26. System.out.println("Consumer: "+message);
  27. buffer.remove(0);
  28. }
  29. cycle++;
  30. }
  31. }
  32. public static void main(String[] args) {
  33. List<String> buffer1 = new ArrayList<String>();
  34. List<String> buffer2 = new ArrayList<String>();
  35. Exchanger<List<String>> exchange = new Exchanger<List<String>>();
  36. Producer producer = new Producer(buffer1, exchange);
  37. Consumer consumer = new Consumer(buffer2, exchange);
  38. Thread threadProducer=new Thread(producer);
  39. Thread threadConsumer=new Thread(consumer);
  40. threadProducer.start();
  41. threadConsumer.start();
  42. }
  43. }
  1. The consumer begins with an empty buffer and calls Exchanger to synchronize with the
  2. producer. It needs data to consume. The producer begins its execution with an empty buffer.
  3. It creates 10 strings, stores it in the buffer, and uses the exchanger to synchronize with
  4. the consumer.
  1. At this point, both threads (producer and consumer) are in Exchanger and it changes the
  2. data structures, so when the consumer returns from the exchange() method, it will have a
  3. buffer with 10 strings. When the producer returns from the exchange() method, it will have
  4. an empty buffer to fill again. This operation will be repeated 10 times.
  1. If you execute the example, you will see how producer and consumer do their jobs
  2. concurrently and how the two objects interchange their buffers in every step. As it occurs with
  3. other synchronization utilities, the first thread that calls the exchange() method was put to
  4. sleep until the other threads arrived.

Java多线程之~~~使用Exchanger在线程之间交换数据[这个结合多线程并行会有解决很多问题]生产者消费者模型的更多相关文章

  1. JAVA多线程学习八-多个线程之间共享数据的方式

    多个线程访问共享对象和数据的方式 如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 如果每个线程执行的代码不同,这 ...

  2. Android多线程研究(5)——线程之间共享数据

    一.如果是每个线程都执行相同的代码,则可以使用同一个Runnable来实现共享 public class MultiThreadShareData { public static void main( ...

  3. Java生产者消费者模型

    在Java中线程同步的经典案例,不同线程对同一个对象同时进行多线程操作,为了保持线程安全,数据结果要是我们期望的结果. 生产者-消费者模型可以很好的解释这个现象:对于公共数据data,初始值为0,多个 ...

  4. java多线程:线程间通信——生产者消费者模型

    一.背景 && 定义 多线程环境下,只要有并发问题,就要保证数据的安全性,一般指的是通过 synchronized 来进行同步. 另一个问题是,多个线程之间如何协作呢? 我们看一个仓库 ...

  5. Java并发工具类(四):线程间交换数据的Exchanger

    简介 Exchanger(交换者)是一个用于线程间协作的工具类.Exchanger用于进行线程间的数据交换.它提供一个同步点,在这个同步点两个线程可以交换彼此的数据.这两个线程通过exchange方法 ...

  6. java 线程池、多线程实战(生产者消费者模型,1 vs 10) 附案例源码

    导读 前二天写了一篇<Java 多线程并发编程>点我直达,放国庆,在家闲着没事,继续写剩下的东西,开干! 线程池 为什么要使用线程池 例如web服务器.数据库服务器.文件服务器或邮件服务器 ...

  7. java线程基础巩固---多线程下的生产者消费者模型,以及详细介绍notifyAll方法

    在上一次[http://www.cnblogs.com/webor2006/p/8419565.html]中演示了多Product多Consumer假死的情况,这次解决假死的情况来实现一个真正的多线程 ...

  8. 第23章 java线程通信——生产者/消费者模型案例

    第23章 java线程通信--生产者/消费者模型案例 1.案例: package com.rocco; /** * 生产者消费者问题,涉及到几个类 * 第一,这个问题本身就是一个类,即主类 * 第二, ...

  9. Java多线程15:Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型

    Queue是什么 队列,是一种数据结构.除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的.无论使用哪种排序方式,队列的头都是调用remove()或poll()移 ...

随机推荐

  1. c++实现快速排序详细分析

    快速排序坑挺多的,今天有空记录一下自己的实现,并加上详细的注释和举例 #include<iostream> using namespace std; int partion(int num ...

  2. Delphi @ # $ 特殊字符含义

      ^: 指针   @: 取址  #: 十进制符   $: 十六进制符

  3. HDU - 3068 最长回文(manacher)

    HDU - 3068 最长回文 Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Subm ...

  4. Apache .htaccess语法之RewriteRule

    [说明]定义重写的规则[语法]RewriteRule Pattern rewritePattern [flags] # 开启 rewrite 功能 Options +FollowSymlinks Re ...

  5. 百度用AR复现朝阳门,野心渐明直指AR平台

    近日,支付宝推出基于"AR+LBS"的AR实景红包后,BAT的另一个巨头百度也忍不住展示了自家AR技术.12月22日上午11点,百度邀请了一众媒体朋友前往朝阳门地铁站F口,体验期最 ...

  6. js 对象 视频 插入元素

    <!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8" ...

  7. uhttpd配置文件分析

    文件位于 /etc/config/uhttpd. root@hbg:/etc/config# cat uhttpd config uhttpd 'main'        list listen_ht ...

  8. Web调用安卓,苹果手机摄像头,本地图片和文件

    由于要给一个客户做一个记账WAP,里面有调用手机拍照功能,这里记录一下,以供需要的朋友,下面是完整的一个HTML页面内容,放在服务器上然后浏览就可以了,只支持Chrome和Safari核的浏览器,我测 ...

  9. 深度探索C++对象模型之C++对象模型笔记

    0.菜鸟觉得,在看这本书的时候最好切换角色,把自己的思维转换成编译器开发者,去考虑问题,这样会容易理解些.(当然这样很难,就想着自己要解决什么样的问题好了) 1.在C++中,类的数据成员有两种:静态和 ...

  10. ural 1355. Bald Spot Revisited(数的素因子划分)

    1355. Bald Spot Revisited Time limit: 1.0 secondMemory limit: 64 MB A student dreamt that he walked ...