基于synchronized实现的阻塞队列
package com.lilei.pack09; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class MySyncQueue<T> { private Object[] ts; int pos = -1; public MySyncQueue(int size) {
ts = new Object[size];
} public synchronized void push(T t) throws InterruptedException {
while (true) {
if (pos + 1 < ts.length) {
ts[++pos] = t;
notifyAll();
System.out.println(Thread.currentThread().getName() + " push,currentSize=" + (pos + 1));
return;
} else {
wait();
}
}
} public synchronized T pop() throws InterruptedException { while (true) {
if (pos >= 0) {
@SuppressWarnings("unchecked")
T t = (T) ts[pos--]; notifyAll(); System.out.println(Thread.currentThread().getName() + " pop,currentSize=" + (pos + 1)); return t;
} else {
wait();
}
} } public static class Inner { } public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(30); final MySyncQueue<Inner> queue = new MySyncQueue<Inner>(15); int repeat = 1000; while (repeat-->0) { for (int i = 0; i < 15; i++) {
es.execute(new Runnable() {
public void run() { try {
queue.pop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
} for (int i = 0; i < 15; i++) {
es.execute(new Runnable() {
public void run() { try {
queue.push(new Inner());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
} } es.shutdown();
} }
基于synchronized实现的阻塞队列的更多相关文章
- 用阻塞队列实现一个生产者消费者模型?synchronized和lock有什么区别?
多线程当中的阻塞队列 主要实现类有 ArrayBlockingQueue是一个基于数组结构的有界阻塞队列,此队列按FIFO原则对元素进行排序 LinkedBlockingQueue是一个基于链表结构的 ...
- java并发:阻塞队列
第一节 阻塞队列 1.1 初识阻塞队列 队列以一种先进先出的方式管理数据,阻塞队列(BlockingQueue)是一个支持两个附加操作的队列,这两个附加的操作是:在队列为空时,获取元素的线程会等待队列 ...
- java高并发系列 - 第25天:掌握JUC中的阻塞队列
这是java高并发系列第25篇文章. 环境:jdk1.8. 本文内容 掌握Queue.BlockingQueue接口中常用的方法 介绍6中阻塞队列,及相关场景示例 重点掌握4种常用的阻塞队列 Queu ...
- Java并发编程之阻塞队列
1.什么是阻塞队列? 队列是一种数据结构,它有两个基本操作:在队列尾部加入一个元素,从队列头部移除一个元素.阻塞队里与普通的队列的区别在于,普通队列不会对当前线程产生阻塞,在面对类似消费者-生产者模型 ...
- 细说并发5:Java 阻塞队列源码分析(下)
上一篇 细说并发4:Java 阻塞队列源码分析(上) 我们了解了 ArrayBlockingQueue, LinkedBlockingQueue 和 PriorityBlockingQueue,这篇文 ...
- Java集合--阻塞队列及各种实现的解析
阻塞队列(Blocking Queue) 一.队列的定义 说的阻塞队列,就先了解下什么是队列,队列也是一种特殊的线性表结构,在线性表的基础上加了一条限制:那就是一端入队列,一端出队列,且需要遵循FIF ...
- Java多线程_JUC包下的阻塞队列
在前面我们提到了阻塞队列,也用过了LinkedBolckingQueue队列了,在这里,我们主要对 ArrayBlockingQueue,PriorityBlockingQueue,DelayQueu ...
- Java多线程_阻塞队列
1.什么是阻塞队列 我们知道,PriorityQueue.LinkedList这些都是非阻塞队列.在我们使用非阻塞队列的时候有一个很大问题,它不会对当前线程产生阻塞,那么在面对类似消费者- ...
- Java并发包源码学习系列:阻塞队列实现之ArrayBlockingQueue源码解析
目录 ArrayBlockingQueue概述 类图结构及重要字段 构造器 出队和入队操作 入队enqueue 出队dequeue 阻塞式操作 E take() 阻塞式获取 void put(E e) ...
随机推荐
- 集成 solr6.5.1到 tomcat7(8) 中 (解决java.lang.NoSuchMethodError问题)
♣下载solr安装包 ♣安装solr ♣solr应用部署到tomcat下 ♣配置web.xml ♣在tomcat7启动 ♣改为在tomcat8启动 ♣在自带的Jetty里启动 ♣建立第一个Core 安 ...
- Hibernate5环境搭建
1.导包 Hibernate开发包 数据库的驱动包 2.核心配置文件 核心配置文件(赋值到src下) 1.核心配置文件 对于hibernate的核心配置文件它有两种方式(选其中一种即可 ...
- java-数据库连接,分层实现增删改查测试
成员属性类: public class Dog { private int number; private String name; private String strain; private St ...
- iBrand 产品工具包:Laravel Database Logger
iBrand 社交新零售电商产品从2016年9月启动至今,已经趋于稳定,而且已经初步得到市场的检验,特别能抗住电商中秒杀时高并发的交易场景. 接下来我们团队会逐步开源一些正在使用的工具和解决方案,并且 ...
- java容器类分析:Collection,List,ArrayList
1. Iterable 与 Iterator Iterable 是个接口,实现此接口使集合对象可以通过迭代器遍历自身元素. public interface Iterable<T> 修饰符 ...
- 笔记︱图像语义分割(FCN、CRF、MRF)、论文延伸(Pixel Objectness、)
图像语义分割的意思就是机器自动分割并识别出图像中的内容,我的理解是抠图- 之前在Faster R-CNN中借用了RPN(region proposal network)选择候选框,但是仅仅是候选框,那 ...
- 使用xfire搭建webService服务
后边有个项目需要接入4A,要用到webService服务,暂时还不确定是不是会有我的事,但为了有备无患,还是抽时间学习了以下相关的知识. 本来我所了解到的发布webService服务有用cxf和xfi ...
- HighCharts之2D堆面积图
HighCharts之2D堆面积图 1.HighCharts之2D堆面积图源码 StackedArea.html: <!DOCTYPE html> <html> <hea ...
- FindBugs找到错误(一)
FindBugs找到错误(一) 错误类型: SBSC_USE_STRINGBUFFER_CONCATENATION
- Error Code: 1175. You are using safe update mode and you tried to update a table
错误描述 11:14:39 delete from t_analy_yhd Error Code: 1175. You are using safe update mode and you tried ...