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 ...
随机推荐
- 非常基本的SQL 内外连接
有些问题一直很郁闷,例如:为什么会存在大约在同一时间连接这个东西.如果外键为空,创建问题的声明时,将有一个外键约束失败. 后来,在精心研究,恩.外部连接(左连接.正确的连接). 事实上都是非常基础的东 ...
- 【iOS发展-61】更换plist经过资源,执行iOS一旦数据仍显示在模拟器的外观,如何解决?
(1)案例介绍 --我们首先导入plist文件做项目,模拟的观看效果. --删除plist,更换一个新的plist,CMD+R模拟执行,或者找到该程序界面上显示最后一个数据. (2)原因 是由于第一次 ...
- GitLab 安装配置笔记(转)
GitLab的安装方式 GitLab的两种安装方法: 编译安装 优点:可定制性强.数据库既可以选择MySQL,也可以选择PostgreSQL;服务器既可以选择Apache,也可以选择Nginx. 缺点 ...
- Namespace:Openstack的网络实现
前言:众所周知在linux系统中PID.IPC.Network等都是全局性的资源,不论什么的改动和删减都会对整个系统造成影响.这也是为什么kvm之类的虚拟化技术须要模拟一个完毕主机系统的原因. 可是. ...
- Tomcat—怎样在Tomcat Webserver下部署Web项目
总结一下怎样在Tomcat Webserver下部署Web项目:
- android 反编译,反,注射LOG
反编译smali注射显示LOG该代码.以后使用: .class public Lnet/iaround/connector/DebugClass; .super Ljava/lang/Object; ...
- 【转】Android 避免APP启动闪黑屏(Theme和Style)
前几天Boss就反应说,机器每次启动程序都会闪一下黑屏,这个客户不接受.没办法,只能想想怎么解决,最后找到了下面的方法.闪黑屏的原因主要是我们启动Activity的时候,需要跑完onCreate和on ...
- PHP与EXCEL PHPExcel
1.PHPExcel一个简短的引论 PHPExcel 它是用来操作Office Excel 文档PHP图书馆,它是基于微软的OpenXML标准PHP语言.能够使用它来读.写不同格电子表的类型格,例如 ...
- ADT后windows菜单未找到Android SDK Manager和Android Virtual Device Manager该解决方案的选择
打开今天凌晨ADT准备编译androidproject的时候,突然发现windows菜单下的Android SDK Manager和Android Virtual Device Manager选项不见 ...
- Arcgis for Javascript之featureLayer图和属性互操作性
说明:主要实现加载FeatureLayer并显示属性表,而要实现联动属性表与地图,首先,看看实施后的效果: 显示效果 如上图所看到的,本文章主要实现了下面几个功能:1.FeatureLayer属性表的 ...