最近对juc挺杆兴趣的,因为个人原因本身挺喜欢多线程,因为JUC是处理高并发的特别好的工具,因此最近一直在学习,希望大家能互相学习,在这是我对一些JUC的一些类的测试代码,
/*这里是对LinkedBlockingQueue的测试
public class TestLinkedBlockingQueue {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();
MyThread t = new MyThread(queue);
for (int i = 0; i < 300; i++) {
new Thread(t).start();
}
try {
Thread.sleep(10000);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println(queue.size());//输出值是稳定的
Iterator<String> it=queue.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
/****这里是我的线程类,主要为了测试其是否支持多线程*****/
public class MyThread implements Runnable {
private LinkedBlockingQueue<String> queue;
public MyThread(LinkedBlockingQueue<String> queue) {
super();
this.queue = queue;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
String s=UUID.randomUUID().toString();
System.out.println("加入数据"+s);
queue.add(s);//如果添加成功返回true;
} catch (Exception e) {
// TODO: handle exception
}
}
}
测试LinkedBlockingQueue
本文通过一个简单的测试案例展示了如何使用Java并发工具包(JUC)中的LinkedBlockingQueue来处理多线程环境下的数据队列管理。具体包括创建队列、多线程数据添加以及队列状态检查等关键操作。
1万+

被折叠的 条评论
为什么被折叠?



