一段阻塞队列代码的纠错与优化
2013-04-16 14:16
405 查看
下面的代码在某处发现后,立马发现存在问题。
最简单的情况就是一个生产者,两个消费者,
上面的if语句在第二线程重新获取锁后,进来的时候不会再次判断是否有消费元素,会直接返回null。
修正代码如上面所示。
整个代码主要用作示例,所以代码上存在很冗余的点,下面是代码优化后的版本:
从性能上优化,可以参考《一种简单无锁队列的实现》和《无锁同步栈实现》。
下面是测试代码:
public class BlockingQ { private Object notEmpty = new Object(); private Object notFull = new Object(); private Queue<Object> linkedList = new LinkedList<Object>(); private int maxLength = 10; public Object take() throws InterruptedException{ synchronized(notEmpty){ if(linkedList.size() == 0){ notEmpty.wait(); } synchronized(notFull){ if(linkedList.size() == maxLength){ notFull.notifyAll(); } return linkedList.poll(); } } } public void offer(Object object) throws InterruptedException{ synchronized(notEmpty){ if(linkedList.size() == 0){ notEmpty.notifyAll(); } synchronized(notFull){ if(linkedList.size() == maxLength){ notFull.wait(); } linkedList.add(object); } } } }
最简单的情况就是一个生产者,两个消费者,
if(linkedList.size() == 0){ notEmpty.wait(); }
上面的if语句在第二线程重新获取锁后,进来的时候不会再次判断是否有消费元素,会直接返回null。
public Object take() throws InterruptedException{ synchronized(notEmpty){ while(linkedList.size() == 0){ notEmpty.wait(); } synchronized(notFull){ if(linkedList.size() == maxLength){ notFull.notifyAll(); } return linkedList.poll(); } } }
修正代码如上面所示。
整个代码主要用作示例,所以代码上存在很冗余的点,下面是代码优化后的版本:
public class SimpleBlockingQ { private Queue<Object> linkedList = new LinkedList<Object>(); private int maxLength = 10; public synchronized Object take() throws InterruptedException{ while(linkedList.size() == 0){ wait(); } notifyAll(); return linkedList.poll(); } public synchronized void offer(Object object) throws InterruptedException{ notifyAll(); if(linkedList.size() == maxLength){ wait(); } linkedList.add(object); } }
从性能上优化,可以参考《一种简单无锁队列的实现》和《无锁同步栈实现》。
下面是测试代码:
import java.util.concurrent.atomic.AtomicInteger; /** * @author 天水 * @date 2013-4-12 下午03:44:15 */ public class BlockingQTest { public static AtomicInteger index = new AtomicInteger(0); public static void main(String[] args){ int tCount = 10; // thread count final BlockingQ BQ = new BlockingQ(); final SimpleBlockingQ SBQ = new SimpleBlockingQ(); // provider Runnable pr = new Runnable(){ @Override public void run() { while(true){ try { Thread.sleep(100); int tindex = index.getAndIncrement(); //BQ.offer(tindex); //System.out.println("BQ offer: " + tindex); SBQ.offer(tindex); System.out.println("SBQ offer: " + tindex); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }; // consumer Runnable cr = new Runnable(){ @Override public void run() { while(true){ try { Thread.sleep(100); //System.out.println("BQ take: " + BQ.take()); System.out.println("SBQ take: " + SBQ.take()); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }; for(int i=0; i<tCount; i++){ new Thread(cr).start(); } for(int i=0; i<tCount; i++){ new Thread(pr).start(); } } }
相关文章推荐
- 一段阻塞队列代码的纠错与优化
- 一段阻塞队列代码的纠错与优化
- 一段阻塞队列代码的纠错与优化
- 一段代码的优化过程
- 【Java并发编程】:并发新特性—阻塞队列和阻塞栈(含代码)
- easyui分页,根据网友的一段代码优化了一下
- SDK程序员经常写出的一段低效率代码[代码优化][原]
- 第二十一篇:并发新特性—阻塞队列和阻塞栈(含代码)
- 由一段代码谈前端js优化和编码规范
- 笔记52 | 优化一段苏基平大神的代码记录
- 单源最短路之spfa算法代码(bellman-ford算法的队列优化)
- 【Java并发编程】之二十一:并发新特性—阻塞队列和阻塞栈(含代码)
- Java并发框架——AQS阻塞队列管理(二)——自旋锁优化
- Java并发编程:阻塞队列(Blocking Queues)、线程池(Thread Pools)的原理与代码实现
- 一段简单代码理解异步请求消息队列在高并发环境中的作用
- 【Java并发编程】并发新特性—阻塞队列和阻塞栈(含代码)
- Windows Xp 优化文件 的一段BAT代码
- Java并发框架——AQS阻塞队列管理(二)——自旋锁优化
- 单调队列优化多重背包(pascal及翻译过去的C代码)
- [改善Java代码]适当设置阻塞队列长度