架构师养成记--8.Queue
一、ConcurrentLinkedQueue
是一个适合在高并发场景下,无锁,无界的,先进先出原则。不允许为null值,add()、offer()加入元素,这两个方法没区别;poll()、peek()取头元素节点,pull会删除,peek不会。
有一点要注意,轮询条件不能用queue.size();而是用 queue.isEmpty(); 看下面的代码:
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ConcurrentLinkedQueueTest {
private static ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();
private static int count = 100000;
private static int count2 = 2; // 线程个数
private static CountDownLatch cd = new CountDownLatch(count2);
public static void dothis() {
for (int i = 0; i < count; i++) {
queue.offer(i);
}
}
public static void main(String[] args) throws InterruptedException {
long timeStart = System.currentTimeMillis();
ExecutorService es = Executors.newFixedThreadPool(4);
ConcurrentLinkedQueueTest.dothis();
for (int i = 0; i < count2; i++) {
es.submit(new Poll());
}
cd.await();
System.out.println("cost time "
+ (System.currentTimeMillis() - timeStart) + "ms");
es.shutdown();
}
static class Poll implements Runnable {
@Override
public void run() {
while (queue.size()>0) {
// while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
cd.countDown();
}
}
}
运行结果是:
改用queue.isEmpty()后运行结果是:
结果居然相差那么大,看了下ConcurrentLinkedQueue的API 原来.size() 是要遍历一遍集合的,难怪那么慢,所以尽量要避免用size而改用isEmpty()。
二、ArrayBlockingQueue
基于数组的阻塞队列、有缓冲、定长、没有实现读写分离。有界队列。
下面是ArrayBlockingQueue实现的生产者消费者模式:
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue; public class ArrayBlockingQueueTest {
public static void main(String[] args) {
BlockingQueue queue = new ArrayBlockingQueue(100);
for (int i = 0; i < 10; i++)
new Thread(new ThreadProducer(queue)).start();
for (int i = 0; i < 10; i++)
new Thread(new ThreadConsumer(queue)).start();
}
} class ThreadProducer implements Runnable {
ThreadProducer(BlockingQueue queue) {
this.queue = queue;
} BlockingQueue queue;
static int cnt = 0; public void run() {
String cmd;
while (true) {
cmd = "" + (cnt);
cnt = (cnt + 1) & 0xFFFFFFFF;
try {
queue.put(cmd);
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} class ThreadConsumer implements Runnable {
ThreadConsumer(BlockingQueue queue) {
this.queue = queue;
} BlockingQueue queue; public void run() {
String cmd;
while (true) {
try {
System.out.println(queue.take());
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
三、LinkedBlockingQueue
基于链表的阻塞队列、有缓冲、读写分离锁(从而实现生产者和消费者操作的完全并行运行)、无界队列
LinkedBlockingQueue实现是线程安全的,实现了FIFO(先进先出)等特性. 是作为生产者消费者的首选,LinkedBlockingQueue 可以指定容量,也可以不指定,不指定的话,默认最大是Integer.MAX_VALUE,其中主要用到put和take方法,put方法在队列满的时候会阻塞直到有队列成员被消费,take方法在队列空的时候会阻塞,直到有队列成员被放进来。
工厂生产制造 生产高大上洒, 还有美女.
消费者有X二代,也有导演.
让消费者抢资源吧.
生产者:
import java.util.UUID;
import java.util.concurrent.BlockingQueue; public class Producer implements Runnable {
private BlockingQueue<String> queue;
private String produce;
public Producer(BlockingQueue<String> queue, String produce) {
this.queue = queue;
if (null != produce)
this.produce = produce;
else this.produce = "null ";
} @Override
public void run() {
String uuid = UUID.randomUUID().toString();
try {
Thread.sleep(200);//生产需要时间
queue.put(produce + " : " + uuid);
System.out.println("Produce \"" + produce + "\" : " + uuid + " " + Thread.currentThread()); } catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
消费者:
import java.util.concurrent.BlockingQueue; public class Consumer implements Runnable {
private BlockingQueue<String> queue;
private String consumer; public Consumer(BlockingQueue<String> queue, String consumer) {
this.queue = queue;
if (null != consumer)
this.consumer = consumer;
else
this.consumer = "null ";
} @Override
public void run() {
try {
String uuid = queue.take();
System.out.println(consumer + " decayed " + uuid
+ " " + Thread.currentThread());
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
调用:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue; public class Tester { public Tester(){
// 队列
LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(10); ExecutorService service = Executors.newCachedThreadPool();
for (int i = 0; i < 6; i++) {
service.submit(new Consumer(queue, "X二代" + i));
service.submit(new Consumer(queue, "导演" + i));
}
for (int i = 0; i < 6; i++) {
service.submit(new Producer(queue, "黄金酒," + i));
service.submit(new Producer(queue, "美女演员" + i));
}
service.shutdown();
}
}
四、SynchronousQueue
没有缓冲的队列,生产者查收的数据或直接被消费者获取并消费。在add前必须take,否则报错。
五、PriorityBlockingQueue
元素必须实现Comparable接口,在第一次调用take方法的时候才会被排序
public class Task implements Comparable<Task>{ private int id ;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} @Override
public int compareTo(Task task) {
return this.id > task.id ? 1 : (this.id < task.id ? -1 : 0);
} public String toString(){
return this.id + "," + this.name;
} } import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.PriorityBlockingQueue; public class UsePriorityBlockingQueue { public static void main(String[] args) throws Exception{ PriorityBlockingQueue<Task> q = new PriorityBlockingQueue<Task>(); Task t1 = new Task();
t1.setId(3);
t1.setName("id为3");
Task t2 = new Task();
t2.setId(4);
t2.setName("id为4");
Task t3 = new Task();
t3.setId(1);
t3.setName("id为1"); //return this.id > task.id ? 1 : 0;
q.add(t1); //
q.add(t2); //
q.add(t3); //1 // 1 3 4
System.out.println("容器:" + q);
System.out.println(q.take().getId());
System.out.println("容器:" + q);
// System.out.println(q.take().getId());
// System.out.println(q.take().getId()); }
}
六、DelayQueue 延迟队列
元素需要实现Delayed接口,DelayQueue是没有大小限制的队列。调用take方法不会立即拿到元素,得等到设定的延迟时间到了才能拿到元素。下面是网吧上网流程使用DelayQueue的例子。
package com.bjsxt.base.coll013; import java.util.concurrent.DelayQueue; public class WangBa implements Runnable { private DelayQueue<Wangmin> queue = new DelayQueue<Wangmin>(); public boolean yinye =true; public void shangji(String name,String id,int money){
Wangmin man = new Wangmin(name, id, 1000 * money + System.currentTimeMillis());
System.out.println("网名"+man.getName()+" 身份证"+man.getId()+"交钱"+money+"块,开始上机...");
this.queue.add(man);
} public void xiaji(Wangmin man){
System.out.println("网名"+man.getName()+" 身份证"+man.getId()+"时间到下机...");
} @Override
public void run() {
while(yinye){
try {
Wangmin man = queue.take();
xiaji(man);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public static void main(String args[]){
try{
System.out.println("网吧开始营业");
WangBa siyu = new WangBa();
Thread shangwang = new Thread(siyu);
shangwang.start(); siyu.shangji("路人甲", "123", 1);
siyu.shangji("路人乙", "234", 10);
siyu.shangji("路人丙", "345", 5);
}
catch(Exception e){
e.printStackTrace();
} }
} -------------------------------------------------------- package com.bjsxt.base.coll013; import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit; public class Wangmin implements Delayed { private String name;
//身份证
private String id;
//截止时间
private long endTime;
//定义时间工具类
private TimeUnit timeUnit = TimeUnit.SECONDS; public Wangmin(String name,String id,long endTime){
this.name=name;
this.id=id;
this.endTime = endTime;
} public String getName(){
return this.name;
} public String getId(){
return this.id;
} /**
* 用来判断是否到了截止时间
*/
@Override
public long getDelay(TimeUnit unit) {
//return unit.convert(endTime, TimeUnit.MILLISECONDS) - unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
return endTime - System.currentTimeMillis();
} /**
* 相互批较排序用
*/
@Override
public int compareTo(Delayed delayed) {
Wangmin w = (Wangmin)delayed;
return this.getDelay(this.timeUnit) - w.getDelay(this.timeUnit) > 0 ? 1:0;
} }
架构师养成记--8.Queue的更多相关文章
- 架构师养成记--11.Executor概述
常用方法 Executors.newFiexdPool(int nThreads);固定线程数量的线程池: Executors.newSingleThreadExecutor();单个线程的线程池: ...
- 架构师养成记--35.redis集群搭建
前记:redis哨兵经验之谈.哨兵做主从切换可能要花费一两秒,这一两秒可能会丢失很多数据.解决方法之一是在java代码中做控制,try catch 到 链接断开的异常就sleep 一两秒钟再conti ...
- 架构师养成记--15.Disruptor并发框架
一.概述 disruptor对于处理并发任务很擅长,曾有人测过,一个线程里1s内可以处理六百万个订单,性能相当感人. 这个框架的结构大概是:数据生产端 --> 缓存 --> 消费端 缓存中 ...
- 架构师养成记--14.重入锁ReentrantLock 和 读写锁 ReentrantReadWriteLock
ReentrantLock 有嗅探锁定和多路分支等功能,其实就是synchronized,wait,notify的升级. this锁定当前对象不方便,于是就有了用new Object()来作为锁的解决 ...
- 架构师养成记--12.Concurrent工具类CyclicBarrier和CountDownLatch
java.util.concurrent.CyclicBarrier 一组线程共同等待,直到达到一个公共屏障点. 举个栗子,百米赛跑中,所有运动员都要等其他运动员都准备好后才能一起跑(假如没有发令员) ...
- 架构师养成记--10.master-worker模式
master-worker模式是一种并行计算模式,分为master进程和worker进程两个部分,master是担任总管角色,worker才是执行具体任务的地方. 总体流程应该是这样的: 具体一点,代 ...
- 架构师养成记--9.future模式讲解
什么是future模式呢?解释这个概念之前我们先来了解一个场景吧,财务系统的结账功能,这个功能可能是每个月用一次,在这一个月中相关的数据量已经积累得非常大,这一个功能需要调用好几个存储过程来完成.假如 ...
- 架构师养成记--6.单例和多线程、ThreadLocal
一.ThreadLocal 使用wait/notify方式实现的线程安全,性能将受到很大影响.解决方案是用空间换时间,不用锁也能实现线程安全. 来看一个小例子,在线程内的set.get就是thread ...
- 架构师养成记--4.volatile关键字
volatile修饰的变量可在多个线程间可见. 如下代码,在子线程运行期间主线程修改属性值并不对子线程产生影响,原因是子线程有自己独立的内存空间,其中有主内存中的变量副本. public class ...
随机推荐
- js 轮播效果
<!--图片轮播 Start--> <div class="pics-ul"> ...
- 【blade利刃出鞘】一起进入移动端webapp开发吧
前言 在移动浪潮袭来的时候,小钗有幸进入框架组做webapp框架开发,过程中遇到了移动端的各种坑,也产生了各种激情,就我们公司的发展历程来说 第一阶段:使用传统方式开发移动站点,少量引入HTML5元素 ...
- 3.2 js六大数据类型
js中有六种数据类型,包括五种基本数据类型(Number,String,Boolean,Null,Undefined),和一种混合数据类型(Object). 前面说到js中变量是松散类型的,因此有时候 ...
- JDBC数据库访问操作的动态监测 之 p6spy
P6spy是一个JDBC Driver的包装工具,p6spy通过对JDBC Driver的封装以达到对SQL语句的监听和分析,以达到各种目的. P6spy1.3 sf.net http://sourc ...
- UITableViewCell定制
UITableViewCell定制 效果 特点 1.可以添加不同的TableViewCell,可以定制不同的cell样式; 2.可以动态改变cell的高度; 3.可以随意摆放你cell的位 ...
- [Erlang 0127] Term sharing in Erlang/OTP 上篇
之前,在 [Erlang 0126] 我们读过的Erlang论文 提到过下面这篇论文: On Preserving Term Sharing in the Erlang Virtual Machine ...
- MySQL 常用命令总结
http://blog.csdn.net/hanxin1987216/article/details/5976860 一.总结 1.Linux系统下启动MySQL的命令: mysqladmin sta ...
- 5-2 bash 脚本编程之一 变量、变量类型等
1. bash变量类型 1. 环境变量 2. 本地变量(局部变量) 3. 位置变量 4. 特殊变量 2. 本地变量 VARNAME=VALUE, 整个bash进程 3. 环境变量 作用域为当前shel ...
- Android 更改 Toast 的默认位置
Android中Toast的默认位置在屏幕靠近底部的位置,这个默认位置有时候并不合适.比如页面上内容较少时,内容一般集中在屏幕上半部分,用户的注意力也集中在屏幕上半部分,默认位置的Toast用户可能没 ...
- LAMP环境配置 linux+apache+mysql+php
虚拟机安装Linux系统: 新建虚拟机过程中选择Linux,下面选择centos或者是Ubuntu Linux切换图像命令:注意只有装了图像界面才可以切换 查看安装环境的版本: rpm -qa 查看安 ...