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多线程测试的更多相关文章

  1. Junit使用GroboUtils进行多线程测试

    写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...

  2. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  3. 关于JUnit4无法支持多线程测试的解决方法

    转自:https://segmentfault.com/a/1190000003762719 其实junit是将test作为参数传递给了TestRunner的main函数.并通过main函数进行执行. ...

  4. TestNG多线程测试-注解方式实现

    用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...

  5. TestNg之XMl形式实现多线程测试

    为什么要使用多线程测试? 在实际测试中,为了节省测试时间,提高测试效率,在实际测试场景中经常会采用多线程的方式去执行,比如爬虫爬数据,多浏览器并行测试. 关于多线程并行测试 TestNG中实现多线程并 ...

  6. JUNIT4 GroboUtils多线程测试

    阅读更多 利用JUNIT4,GroboUtils进行多线程测试 多线程编程和测试一直是比较难搞的事情,特别是多线程测试.只用充分的测试,才可以发现多线程编码的潜在BUG.下面就介绍一下我自己在测试多线 ...

  7. TestNG 多线程测试

    TestNG以注解的方式实现多线程测试 import org.testng.annotations.Test; public class TreadDemo { // invocationCount ...

  8. 【多线程】java多线程 测试例子 详解wait() sleep() notify() start() join()方法 等

    java实现多线程,有两种方法: 1>实现多线程,继承Thread,资源不能共享 2>实现多线程  实现Runnable接口,可以实现资源共享 *wait()方法 在哪个线程中调用 则当前 ...

  9. C语言 多线程测试

    1.CreateThread 在主线程的基础上创建一个新线程 2.WaitForMultipleObjects 主线程等待子线程 3.CloseHandle 关闭线程 // testThread.cp ...

随机推荐

  1. asp.net在用户控件中使用ClientScript

    在用户空间中调用ClientScript.RegisterClientScriptBlock方法 ClientScript的命名空间是System.Web.UI.Page,并且要实例化之后的Page才 ...

  2. .Net下一个Winform方案可以让MessageBox.Show它显示在父窗口的中间

    下面的文字,缺省值是在屏幕中间显示. DialogResult dr = MessageBox.Show("是否要删除此数据?", "删除确认", Messag ...

  3. paip.java UrlRewrite 的原理and实现 htaccess正則表達式转换

    paip.java UrlRewrite 的原理and实现 htaccess正則表達式转换 #---KEYWORD #-正則表達式 正則表達式 表示 非指定字符串开头的正则 排除指定文件夹.. 作者 ...

  4. sails不是内部或外部命令的解决方案

    1 安装好node 2 安装sails 打开cmd窗口,用命令 npm -g install sails 安装sails 安装完成后,用命令  sails new testProject 创建项目 会 ...

  5. C# winForm里窗体嵌套

    ShowAllPage sAllPage = new ShowAllPage();            sAllPage.FormBorderStyle = FormBorderStyle.None ...

  6. 【C语言探索之旅】 第一部分第四课第二章:变量的世界之变量声明

    内容简介 1.课程大纲 2.第一部分第四课第二章:变量的世界之变量声明 3.第一部分第四课第三章预告:变量的世界之显示变量内容 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布 ...

  7. spring集成quartz

    spring集成quartz 注意:出现异常"Caused by: java.lang.IncompatibleClassChangeError: class org.springframe ...

  8. ajaxFileUpload+struts2多文件上传(动态添加文件上传框)

    上一篇文章http://blog.csdn.net/itmyhome1990/article/details/36396291介绍了ajaxfileupload实现多文件上传, 但仅仅是固定的文件个数 ...

  9. 相关Python分割操作

    刚论坛python文本 http://bbs.byr.cn/#!article/Python/1693 攻克了一个关于python分片的问题. 问题: uesrList = ['1','2','3', ...

  10. OpenGL【2 坐标转换】

    // OpenGL.cpp : 自定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include & ...