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 ...
随机推荐
- ueditor问题解决
ueditor图片无法上传? 解决: imageUp.ashx 去掉这一行 <%@ Assembly Src="Uploader.cs" %> 参考: http://w ...
- java: org.luaj.vm2.LuaError:XXX module not found lua脚本初始化出错(转)
我遇到这个错误是因为在引用脚本目录时,设置错了位置.设置成脚本所在目录的上级目录. lua使用和加载初始化方法 在java中使用lua,使用需要引用 luaj-jse-2.0.2.jar 同时需要使用 ...
- $POST 、$HTTP_RAW_POST_DATA、php://input三者之间的差别
$POST .$HTTP_RAW_POST_DATA.php://input三者之间的差别 总是产生变量包括有原始的 POST 数据.否则,此变量仅在碰到未识别 MIME 类型的数据时产生.只是,訪问 ...
- 王立平--怎么查看Unity的版本号
1.打开Unity,Help->About Unity 2.版本
- html/css获得第一章
1.基本教程来学习 大概3天课余时间阅读下面的两个教程. HTML文字教程 CSS文字教程 2.练习 看完教程后.做第一练习时,总结例如以下: 1)div居中 须要设置属性:margin-left:a ...
- Windows 8实例教程系列 - 开篇
原文:Windows 8实例教程系列 - 开篇 2012年10月26日,微软发布Windows 8操作系统以及自主品牌平板电脑Surface,Windows作为世界上最流行的操作系统,发布一周内,下载 ...
- 在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址?
在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址? 使用 dmesg 与 grep 命令来实际,例如以下: [root@localhost ~]# dmesg | grep eth e10 ...
- cocos2d-x 3.1.1 学习笔记[17] 关于这些活动功能
供cocos2d-x通常使用的方法,我有一个好脸色.这项研究真的奖励. 向导首先,定义,实施一系列连续动作. 对于我们的行动能回调函数,我们必须申报并加以实施. void callBack(); vo ...
- SqlServer中存储过程中将Exec的执行结果赋值给变量输出
原文 SqlServer中存储过程中将Exec的执行结果赋值给变量输出 背景: 遇到这样一种情况:动态向存储过程中传入表名和表的某些属性(这里用到的是主键ID),然后利用这两个变量查出一条数据的某些字 ...
- 【转】ubuntu终端方向键不能用(主机名不显示)问题的解决
sudo gedit /etc/passwd 在/etc/passwd中修改该用户对应的shell:/bin/sh改为/bin/bash即可解决该问题 来自:http://blog.csdn.net/ ...