- 浏览: 149893 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
hardPass:
貌似二分法,没有一个合并的过程
简单_分治算法 -
zhufeng1981:
讲解的不错,支持一下。
简单_分治算法 -
a346063587:
嗯。。的确,基础很重要!
关于递归和尾递归的原理 -
zhufeng1981:
huoyj 写道基础很重要,这是永远不变的真理。 很赞同这句话 ...
关于递归和尾递归的原理 -
huoyj:
基础很重要,这是永远不变的真理。 很赞同这句话
关于递归和尾递归的原理
官方的java.util.concurrent.ArrayBlockingQueue的性能是很BT的,我下午无聊然后就想去测试下到底有多BT就写了如下测试代码,也不知道是我的代码写的有问题还是怎么的啦,测试结果和我想的完全不一样。
条件:20个线程,存取线程各半,队列大小是30W,其他电脑配置啥的啊很大众化就不描述了。
耗时:
山寨版的:2400左右
官方版的:3400左右
--------------------------------------------------------------------------------------------
官方的java.util.concurrent.ArrayBlockingQueue 生产者消费者代码如下:
自己实现的同步队列如下:
补充:
为什么synchronized反而比ReentrantLock还要快?这个问题终于自己找到了答案。
jdk.16下synchronized和ReentrantLock的速度差不多,似乎还快一些,但是synchronized的缺点很多 ,参考http://houlinyan.iteye.com/blog/1112535两者的对比。
jdk1.5下ReentrantLock快synchronized接近9-10倍(非专业测试仅供参考)。
条件:20个线程,存取线程各半,队列大小是30W,其他电脑配置啥的啊很大众化就不描述了。
耗时:
山寨版的:2400左右
官方版的:3400左右
--------------------------------------------------------------------------------------------
官方的java.util.concurrent.ArrayBlockingQueue 生产者消费者代码如下:
package thread; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class ProducerCustomerQueue2<E> { public static void main(String[] args) throws InterruptedException { /** * 20个线程,存取线程各半,队列大小是30W,耗时:2400左右 */ int total = 300000, threadCount = 20,queueSize = 100; ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(queueSize); final long start = System.currentTimeMillis(); CyclicBarrier barrier = new CyclicBarrier(threadCount, new Runnable() { public void run() { System.out.println("统计时间:" + (System.currentTimeMillis() - start)); } }); for (int i = 0; i < threadCount >>> 1; i++) { new Thread(new ProducerThread2(queue, barrier, total)).start(); new Thread(new CustomerThread2(queue, barrier, total)).start(); } } } // 生产者线程 class ProducerThread2 implements Runnable { ArrayBlockingQueue<Integer> queue; CyclicBarrier barrier; int total; public ProducerThread2(ArrayBlockingQueue<Integer> queue, CyclicBarrier barrier, int total) { super(); this.queue = queue; this.barrier = barrier; this.total = total; } public void run() { System.out.println("ProducerThread启动..."); for (int i = 0; i < total; i++) { try { queue.put(i); } catch (InterruptedException e) { e.printStackTrace(); } } try { System.out.println("ProducerThread完毕!"); barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } // 消费者线程 class CustomerThread2 implements Runnable { ArrayBlockingQueue<Integer> queue; CyclicBarrier barrier; int total; public CustomerThread2(ArrayBlockingQueue<Integer> queue, CyclicBarrier barrier, int total) { super(); this.queue = queue; this.barrier = barrier; this.total = total; } public void run() { System.out.println("CustomerThread启动..."); for (int i = 0; i < total; i++) { Object o; try { o = queue.take(); } catch (InterruptedException e) { e.printStackTrace(); } } try { System.out.println("CustomerThread完毕!"); barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } }
自己实现的同步队列如下:
package thread; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; public class ProducerCustomerQueue<E> { public static void main(String[] args) throws InterruptedException { /** * 20个线程,存取线程各半,队列大小是30W,耗时:2400左右 */ int total = 300000, threadCount = 20, queueSize = 100; ProducerCustomerQueue<Integer> queue = new ProducerCustomerQueue<Integer>( queueSize); final long start = System.currentTimeMillis(); CyclicBarrier barrier = new CyclicBarrier(threadCount, new Runnable() { public void run() { System.out.println("统计时间:" + (System.currentTimeMillis() - start)); } }); for (int i = 0; i < threadCount >>> 1; i++) { new Thread(new ProducerThread(queue, barrier, total)).start(); new Thread(new CustomerThread(queue, barrier, total)).start(); } } private Object lock = new Object(); private final E[] array; private int putIndex; private int takeIndex; private int count; public synchronized int size() { return count; } public ProducerCustomerQueue(int n) { array = (E[]) new Object[n]; } /** * 将指定的元素插入到此队列的尾部(如果立即可行且不会超过该队列的容量),在成功时返回 true,如果此队列已满,则返回 false。 * * @param o */ public boolean offer(E o) { if (o == null) { throw new NullPointerException(); } synchronized (lock) { if (count == array.length) {// 队列已满 return false; } else { insert(o); return true; } } } /** * 将指定的元素插入此队列的尾部,如果该队列已满,则在到达指定的等待时间之前等待可用的空间。 * * @param o * @throws InterruptedException */ public boolean offer(E o, long timeout) throws InterruptedException { if (o == null) { throw new NullPointerException(); } synchronized (lock) { if (count == array.length) {// 队列已满 lock.wait(timeout); } if (count == array.length) { return false; } else { insert(o); } } return false; } /** * 将指定的元素插入此队列的尾部,如果该队列已满,则等待可用的空间。 * * @param o * @throws InterruptedException */ public void put(E o) throws InterruptedException { synchronized (lock) { while (count == array.length) {// 队列已满 // System.out.println("put wait"); lock.wait(); } insert(o); } } private void insert(E o) { array[putIndex] = o; putIndex = (++putIndex == array.length) ? 0 : putIndex; count++; lock.notify(); } /** * 获取并移除此队列的头,如果此队列为空,则返回 null。 */ public E poll() { synchronized (lock) { if (count == 0) {// 空队列 return null; } return extract(); } } /** * 获取并移除此队列的头部,在指定的等待时间前等待可用的元素(如果有必要)。 * * @throws InterruptedException */ public E poll(long timeout) throws InterruptedException { synchronized (lock) { if (count == 0) {// 空队列 lock.wait(timeout); } if (count == 0) { return null; } else { return extract(); } } } /** * 获取并移除此队列的头部,在元素变得可用之前一直等待(如果有必要)。 * * @return * @throws InterruptedException */ public E take() throws InterruptedException { synchronized (lock) { for (;;) { if (count == 0) {// 空队列 lock.wait(); } else { return extract(); } } } } private E extract() { E o = array[takeIndex]; takeIndex = (++takeIndex == array.length) ? 0 : takeIndex; count--; lock.notify(); return o; } } // 生产者线程 class ProducerThread implements Runnable { ProducerCustomerQueue queue; CyclicBarrier barrier; int total; public ProducerThread(ProducerCustomerQueue queue, CyclicBarrier barrier, int total) { super(); this.queue = queue; this.barrier = barrier; this.total = total; } public void run() { System.out.println("ProducerThread启动..."); for (int i = 0; i < total; i++) { try { // System.out.println("ProducerThread.." + queue.size()); queue.put(i); } catch (InterruptedException e) { e.printStackTrace(); } } try { System.out.println("ProducerThread完毕!"); barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } // 消费者线程 class CustomerThread implements Runnable { ProducerCustomerQueue queue; CyclicBarrier barrier; int total; public CustomerThread(ProducerCustomerQueue queue, CyclicBarrier barrier, int total) { super(); this.queue = queue; this.barrier = barrier; this.total = total; } public void run() { System.out.println("CustomerThread启动..."); for (int i = 0; i < total; i++) { Object o; try { o = queue.take(); } catch (InterruptedException e) { e.printStackTrace(); } } try { System.out.println("CustomerThread完毕!"); barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } }
补充:
为什么synchronized反而比ReentrantLock还要快?这个问题终于自己找到了答案。
jdk.16下synchronized和ReentrantLock的速度差不多,似乎还快一些,但是synchronized的缺点很多 ,参考http://houlinyan.iteye.com/blog/1112535两者的对比。
jdk1.5下ReentrantLock快synchronized接近9-10倍(非专业测试仅供参考)。
发表评论
-
高效编写JAVA代码的几条建议(转)
2011-11-23 20:34 1321(1) 类名首字母应该大写 ... -
java正则表达式及java.util.regex包的学习
2011-11-16 22:34 24727没有事做,就会堕落。。 这个世界有太多的浮躁,要耐得住诱惑; ... -
quartz的配置(转自网络)
2011-11-16 20:18 1169周允许的是1-7 其中1 ... -
[转自网络]freeMarker的常用语法(以后可能会用到)
2011-11-16 09:48 1167常用语法 EG.一个对象BOOK 1.输出 $…{book ... -
开源定时器quartz入门
2011-11-15 20:03 4811闲来无事学习了下quartz框架,其实JAVA下的定时器框架还 ... -
freeMarker入门
2011-11-14 22:26 1492freeMarker是一个 JAVA开源模版引擎 下面以创建 ... -
java.util.logging包的学习
2011-11-11 22:07 1281package sunfa.lx; import jav ... -
mybatis中的一个OOXX
2011-11-08 15:11 1200mybatis : mybatis XML中执行多条语句: ... -
Timer和ScheduledExecutorService区别
2011-10-09 23:59 3168Timer里面的任务如果执行时间太长,会独占Timer对象,使 ... -
java io流之 装饰模式
2011-10-05 21:41 1119初学java.io的时候容易被众多的IO类搞晕头,其实java ... -
java nio学习笔记<一>
2011-10-05 21:04 1058package nio; import java.i ... -
java.util.concurrent.atomic.*包的体会
2011-10-01 18:10 1431java.util.concurrent.atomic.*包的 ... -
计算很大数组中List的和
2011-09-30 19:45 1026原帖地址: http://www.iteye.com/topi ... -
CountDownLatch、CyclicBarrier让多线程变得更简单
2011-09-29 16:54 1071CountDownLatch 一个同步辅助类,在完成一组正在其 ... -
突然发现自己的JAVA基础很差
2011-09-25 11:50 1132今天因为一个问题上网搜索却牵扯出了另一个问题。。。纠结。、、还 ... -
java_Comparable & Comparator
2011-09-21 23:21 1375java有2个非常重要的排序接口:java.lang.Comp ... -
tomcat,jboss部署方式(热部署)
2011-06-26 16:08 2804tomcat: 1、直接把项目web文件夹放在webapps里 ... -
JAVA书籍(IO多线程等)
2011-05-29 15:47 970留下这些书,纪念我曾经走过的路。 -
java IO的学习总结
2011-05-29 15:31 10131、JAVA中的IO流分字节流和字符流 2、InputStre ... -
关于JAVA3D游戏(摘自网络)
2011-01-02 16:56 23352006年8月,我们终于决定用计算机三维视觉游戏机制制作自己的 ...
相关推荐
ArrayBlockingQueue源码分析.docx
在前面的的文章,写了一个带有缓冲区的队列,是用JAVA的Lock下的Condition实现的,但是JAVA类中提供了这项功能,是ArrayBlockingQueue, ArrayBlockingQueue是由数组支持的有界阻塞队列,次队列按照FIFO(先进先...
今天小编就为大家分享一篇关于Java源码解析阻塞队列ArrayBlockingQueue常用方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
今天小编就为大家分享一篇关于Java源码解析阻塞队列ArrayBlockingQueue介绍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
java中,常用的阻塞式队列Demo。包含:ArrayBlockingQueue、LinkedQueue、PriorityBlockingQueue
数组阻塞队列ArrayBlockingQueue,延迟队列DelayQueue, 链阻塞队列 LinkedBlockingQueue,具有优先级的阻塞队列 PriorityBlockingQueue, 同步队列 SynchronousQueue,阻塞双端队列 BlockingDeque, 链阻塞双端队列 ...
今天小编就为大家分享一篇关于Java源码解析阻塞队列ArrayBlockingQueue功能简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
ArrayBlockingQueue源码解析__动力节点共23页.pdf.zip
ArrayBlockingQueue是数组实现的线程安全的有界的阻塞队列。下面通过本文给大家介绍Java concurrency集合之ArrayBlockingQueue的相关知识,感兴趣的朋友一起看看吧
并发容器之ArrayBlockingQueue和LinkedBlockingQueue实现原理详解
主要介绍了详细分析Java并发集合ArrayBlockingQueue的用法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了java并发之ArrayBlockingQueue详细介绍的相关资料,需要的朋友可以参考下
Java并发编程学习宝典(漫画版),Java并发编程学习宝典(漫画版)Java并发编程学习宝典(漫画版)Java并发编程学习宝典(漫画版)Java并发编程学习宝典(漫画版)Java并发编程学习宝典(漫画版)Java并发编程学习...
3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 PriorityBlockingQueue 7. 同步队列 Synchronou sQueue 8. 阻塞双端队列 BlockingDeque 9...
主要介绍了java中LinkedBlockingQueue与ArrayBlockingQueue的异同,需要的朋友可以参考下
3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 PriorityBlockingQueue 7. 同步队列 SynchronousQueue 8. 阻塞双端队列 BlockingDeque 9. ...
数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 PriorityBlockingQueue 7. 同步队列 SynchronousQueue 8. 阻塞双端队列 BlockingDeque 9. 链...