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 ...
随机推荐
- Unity 捕获IronPython脚本错误
using System; using System.Collections.Generic; using System.IO; using System.Reflection; using Syst ...
- 浅谈http请求数据分析
前段时间,我一个朋友给我打了个电话.说是现在在搞网络销售,问我能不能帮他整个自动发帖机.说实在的,以前没有弄过这块,我就跟他讲我试试看吧,能不能成不能保证.毕竟是搞程序的嘛,自学的能力还是有滴.经过一 ...
- freemarker错误七
1.错误叙述性说明 五月 30, 2014 11:33:57 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template p ...
- [渣译文] SignalR 2.0 系列: 开始使用SignalR 2.0
原文:[渣译文] SignalR 2.0 系列: 开始使用SignalR 2.0 英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with ASP ...
- IOS基金会_ UICollectionView简单易用
和表格视图类似 UICollectionView的使用有两种方法 一种是继承UICollectionViewController,这个Controller会自带一个UICollectionView. ...
- [Ext JS 4] 实战Chart 协调控制(单一的坐标,两个坐标)
前言
- IT该忍者神龟Jquery小工具easyUI物业摘要召回
找了个时间看了下EasyUI插件.对它的插件感觉是非常舒服,特地把Easy UI的大部分功能属性做了一下汇总. 此属性列表请对比jQuery EasyUI 1.0.5,关于它的很多其它资讯请猛击这里. ...
- 导出文本pdf文件
出口手续往往是一些数据需求,学习文本导出到今天pdf文件.主要用于QPrinter,QPainter TextEditToPdf::TextEditToPdf(QWidget *parent, Qt: ...
- Git现实(一个)版本控制概述
从今天开始.我们了解的分布式版本控制系统Git相关内容.了解Git之前,我们的第一个版本控制系统,使宏观的描述. 什么是版本号控制 版本号控制是指通过对文件内容的变化进行记录,并为每次的变化进行编号, ...
- height/innerHeight/outerHeight
<script> $(document).ready(function(){ alert("height:"+$("#div").height()) ...