LinkedBlockingQueue多线程测试
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();
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
LinkedBlockingQueue多线程测试的更多相关文章
- Junit使用GroboUtils进行多线程测试
写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...
- testng入门教程12 TestNG执行多线程测试
testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...
- 关于JUnit4无法支持多线程测试的解决方法
转自:https://segmentfault.com/a/1190000003762719 其实junit是将test作为参数传递给了TestRunner的main函数.并通过main函数进行执行. ...
- TestNG多线程测试-注解方式实现
用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...
- TestNg之XMl形式实现多线程测试
为什么要使用多线程测试? 在实际测试中,为了节省测试时间,提高测试效率,在实际测试场景中经常会采用多线程的方式去执行,比如爬虫爬数据,多浏览器并行测试. 关于多线程并行测试 TestNG中实现多线程并 ...
- JUNIT4 GroboUtils多线程测试
阅读更多 利用JUNIT4,GroboUtils进行多线程测试 多线程编程和测试一直是比较难搞的事情,特别是多线程测试.只用充分的测试,才可以发现多线程编码的潜在BUG.下面就介绍一下我自己在测试多线 ...
- TestNG 多线程测试
TestNG以注解的方式实现多线程测试 import org.testng.annotations.Test; public class TreadDemo { // invocationCount ...
- 【多线程】java多线程 测试例子 详解wait() sleep() notify() start() join()方法 等
java实现多线程,有两种方法: 1>实现多线程,继承Thread,资源不能共享 2>实现多线程 实现Runnable接口,可以实现资源共享 *wait()方法 在哪个线程中调用 则当前 ...
- C语言 多线程测试
1.CreateThread 在主线程的基础上创建一个新线程 2.WaitForMultipleObjects 主线程等待子线程 3.CloseHandle 关闭线程 // testThread.cp ...
随机推荐
- Directx11学习笔记【十九】 摄像机的实现
本文由zhangbaochong原创,转载请注明出处:http://www.cnblogs.com/zhangbaochong/p/5785100.html 之前为了方便观察场景,我们采用的方法是鼠标 ...
- Java获取的一天、本星期、这个月、本季度、一年等 开始和结束时间
1.代码 package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- EmEditor Professional(文本编辑) 下载地址
http://www.greenxf.com/soft/2126.html 16.1.5 http://www.cr173.com/soft/3031.html 16.3.0
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
- NYOJ 300 && hdu 2276 Kiki & Little Kiki 2 (矩阵高速功率)
pid=300">Kiki & Little Kiki 2 时间限制:5000 ms | 内存限制:65535 KB 难度:4 描写叙述 There are n light ...
- PHP关联数组和哈希表(hash table) 未指定
PHP有数据的一个非常重要的一类,就是关联数组.又称为哈希表(hash table),是一种很好用的数据结构. 在程序中.我们可能会遇到须要消重的问题,举一个最简单的模型: 有一份username列表 ...
- 开源Math.NET基础数学类库使用(13)C#实现其他随机数生成器
原文:[原创]开源Math.NET基础数学类库使用(13)C#实现其他随机数生成器 本博客所有文章分类的总目录:http://www.cnblogs.com/asxiny ...
- asp.net模板控件示例
原文:asp.net模板控件示例 模板控件允许将控件数据与其表示形式相分离,模板化控件不提供用户界面. 编写它则是为了实现一个命名容器以及包含属性和方法可由宿主页访问的类,MSDN是这样解释的. 下面 ...
- DisplayContent、StackBox、TaskStack笔记
文章仅零散记录自己的一点理解,仅供自己參考. 每一个显示设备,都有一个Display对象,DisplayManagerService专门管理这些Display. 1.DisplayContent() ...
- 【PLSQL】变量声明,结构语句,cursor游标
************************************************************************ ****原文:blog.csdn.net/clar ...