LinkedBlockingQueue多线程测试
2015-08-22 14:13
441 查看
public class FillQueueThread extends Thread { private Queue queue; public FillQueueThread(Queue queue){ this.queue = queue; } @Override public void run() { while(true){ try { boolean added = queue.offer(UUID.randomUUID().toString()); if(added) { System.out.println(Thread.currentThread().getName()+" add 1 element"); }else{ System.out.println(Thread.currentThread().getName()+" is blocked, wait"); //this.wait(); //no need to invoked wait } Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public class PollQueueThread extends Thread { private Queue queue; public PollQueueThread(Queue queue){ this.queue = queue; } @Override public void run() { while(true){ try { Object el = queue.poll(); if(null == el){ System.out.println(Thread.currentThread().getName()+" is blocked, wait"); //this.wait(); //no need to invoked wait }else{ System.out.println(Thread.currentThread().getName()+" pool 1 element"); } Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public class MonitorQueueThread extends Thread { private Queue queue; public MonitorQueueThread(Queue queue){ this.queue = queue; } @Override public void run() { while(true){ System.err.println("queue size:"+queue.size()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }
public class HelloQueue { public static void main(String[] args) { //ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(500,true); LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(500); int threadFillNumber = 10; int threadPollNumber = 3; for(int i=0; i<threadFillNumber; i++){ FillQueueThread th = new FillQueueThread(queue); th.start(); } for(int i=0; i<threadPollNumber; i++){ PollQueueThread th = new PollQueueThread(queue); th.start(); } MonitorQueueThread monitor = new MonitorQueueThread(queue); monitor.start(); } }
相关文章推荐
- Qt:在Quick中使用QZXing库做二维码扫描
- Burp Suite--攻击web 应用程序的集成平台
- uva 11235 Frequent values RMQ问题
- String 类型基本转换 valueOf
- java String,StringBuffer和StringBuilder
- UIImage的两个方法的内存分析
- view 在父view 拖动不过边界(UIPanGestureRecognizer)
- 算法-QuickSort
- [LeetCode] N-Queens II
- UICollectionview Xib 行间距
- ListView UI 混乱问题
- [LeetCode] N-Queens
- Ubuntu system build
- Juerry 实现让复选框实现单选
- UI的初步知识
- hdu 5412 CRB and Queries 2015多校联合训练赛#10 分治 求区间第k大数
- UISegmentControl
- UILabel 根据内容的多少来计算label的frame
- Guest与virtio netdev交互模式
- POJ 2478 Farey Sequence