博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BlockingQueue的使用
阅读量:7098 次
发布时间:2019-06-28

本文共 3459 字,大约阅读时间需要 11 分钟。

一个线程从队列里取数据,一个线程往队列里添加数据

import java.util.concurrent.Exchanger;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class ExchangerTest {    public static void main(String[] args) {        ExecutorService service = Executors.newCachedThreadPool();        final Exchanger
 exchanger = new Exchanger
();        service.execute(new Runnable() {            @Override            public void run() {                try {                    String data1 = "zxx";                    System.out.println("线程" + Thread.currentThread().getName()                            + "正在把数据" + data1 + "换出去");                    Thread.sleep((long) Math.random() * 10000);                                        String data2=(String) exchanger.exchange(data1);                    System.out.println("线程" + Thread.currentThread().getName()                            + "换回的数据为" + data2);                } catch (Exception e) {                    e.printStackTrace();                }            }        });        service.execute(new Runnable() {            @Override            public void run() {                try {                    String data1 = "lhm";                    System.out.println("线程" + Thread.currentThread().getName()                            + "正在把数据" + data1 + "换出去");                    Thread.sleep((long) Math.random() * 10000);                                        String data2=(String) exchanger.exchange(data1);                    System.out.println("线程" + Thread.currentThread().getName()                            + "换回的数据为" + data2);                } catch (Exception e) {                    e.printStackTrace();                }            }        });    }}

长度为1的阻塞队列实现同步通知

import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class BlockingQueueCondition {    public static void main(String[] args) {        ExecutorService service = Executors.newSingleThreadExecutor();        final Business3 business = new Business3();        service.execute(new Runnable(){            public void run() {                for(int i=0;i<50;i++){                    business.sub();                }            }                    });                for(int i=0;i<50;i++){            business.main();        }    }}class Business3{    BlockingQueue
 subQueue = new ArrayBlockingQueue
(1);    BlockingQueue
 mainQueue = new ArrayBlockingQueue
(1);    //这里是匿名构造方法,只要new一个对象都会调用这个匿名构造方法,它与静态块不同,静态块只会执行一次,    //在类第一次加载到JVM的时候执行    //这里主要是让main线程首先put一个,就有东西可以取,如果不加这个匿名构造方法put一个的话程序就死锁了    {        try {            mainQueue.put(1);        } catch (InterruptedException e) {            e.printStackTrace();        }    }    public void sub(){        try        {            mainQueue.take();            for(int i=0;i<10;i++){                System.out.println(Thread.currentThread().getName() + " : " + i);            }            subQueue.put(1);        }catch(Exception e){        }    }        public void main(){                try        {            subQueue.take();            for(int i=0;i<5;i++){                System.out.println(Thread.currentThread().getName() + " : " + i);            }            mainQueue.put(1);        }catch(Exception e){        }            }}

本文出自 “” 博客,请务必保留此出处

转载地址:http://curql.baihongyu.com/

你可能感兴趣的文章
Storm-源码分析-acker (backtype.storm.daemon.acker)
查看>>
《C++编程惯用法——高级程序员常用方法和技巧》——1.6 问题
查看>>
在Linux命令行下令人惊叹的惊叹号(!)
查看>>
深一层看依赖注入
查看>>
【原创】erlang 模块之 os
查看>>
开发工具,编译器控件
查看>>
11g RAC的卸载
查看>>
linux驱动之ioctl
查看>>
redhat nginx 安装
查看>>
Spark 2.x kafka LocationStrategies 的几种方式
查看>>
把非透明swf动画dreamweaver做成透明背景flash动画方法
查看>>
solr相关查询参数
查看>>
PHP设计模式学习笔记: 模版方法
查看>>
实现虚拟机linux共享上网(利用NAT)
查看>>
阿里云的maven代理,比较快
查看>>
浅谈finally使用坑。
查看>>
Python协程深入理解
查看>>
【iphone游戏开发】iphone-Cocos2D游戏开发之一:游戏术语大解析
查看>>
JavaRebel的简单配置
查看>>
获取Extjs文本域中的内容
查看>>