当前位置:网站首页>3 pairs of easy to mix APIs for linkedblockingqueue
3 pairs of easy to mix APIs for linkedblockingqueue
2022-07-22 04:04:00 【jwolf2】
Catalog
3 Yes API Conclusion
Level. | Put in | Take out | ||
---|---|---|---|---|
1 | add | The queue is full ? abnormal :return true | remove | The queue is empty ? abnormal :return head |
2 | offer | The queue is full ?return false:return true | poll | The queue is empty ?return null:return head |
3 | put | The queue is full ? Blocking :return [void] | take | The queue is empty ? Blocking :return head |
Put in test
public static void main(String[] args) throws Exception {
new Thread(() -> {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(1);
try {
queue.put("1");
queue.put("1"); // Blocking when full
System.out.println(" Don't print me ");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(1);
System.out.println("offer1"+queue.offer("1"));
System.out.println("offer2"+queue.offer("1"));// Return when full false
}).start();
new Thread(() -> {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(1);
System.out.println("add1"+queue.add("1"));
System.out.println("add2"+queue.add("1"));// Abnormal when full
}).start();
}
offer1true
offer2false
add1true
Exception in thread "Thread-2" java.lang.IllegalStateException: Queue full
at java.util.AbstractQueue.add(AbstractQueue.java:98)
at com.construn.vehicle.common.core.monitor.App.lambda$main$2(App.java:30)
at com.construn.vehicle.common.core.monitor.App$$Lambda$3/1982791261.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
public static void main(String[] args) throws Exception {
new Thread(() -> {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(Lists.newArrayList("AAA"));
try {
System.out.println("take1"+queue.take());
System.out.println("take2"+queue.take()); // Space time blocking
System.out.println(" Don't print me ");
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(Lists.newArrayList("AAA"));
System.out.println("poll1"+queue.poll());
System.out.println("poll2"+queue.poll());// Empty time returns null
}).start();
new Thread(() -> {
BlockingQueue<String> queue = new LinkedBlockingQueue<>(Lists.newArrayList("AAA"));
System.out.println("remove1"+queue.remove());
System.out.println("remove2"+queue.remove());// Space time exception
}).start();
}
remove1AAA
poll1AAA
poll2null
take1AAA
Exception in thread "Thread-2" java.util.NoSuchElementException
at java.util.AbstractQueue.remove(AbstractQueue.java:117)
at com.construn.vehicle.common.core.monitor.App.lambda$main$2(App.java:54)
at com.construn.vehicle.common.core.monitor.App$$Lambda$3/1982791261.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
边栏推荐
猜你喜欢
随机推荐
Chapter III after class exercises 24-33
BUUCTF-pwn[1]
PCIE链路初始化&训练
一款免费听音乐和下载的播放器
明星餐饮:一场围猎粉丝的杀猪盘
10 papers of ant security laboratory were included by ccf-a top meeting to explore the realization of AI credibility from the perspective of algorithm
互联网大厂为何都爱“送外卖”?
Scala函数的柯里化
【CCF CSP】201409-1相邻数对
2.3 chain representation of linear table (1)
数据模型子类化参考
做做C#
idea报错求助!!!!
知识蒸馏是什么?一份入门随笔__摘要
论文阅读:A Large-Scale Chinese Short-Text Conversation Dataset(CDial-GPT)
Countdownlatch, cyclicbarrier, semaphore of concurrent programming
论文阅读:CTRL: A CONDITIONAL TRANSFORMER LANGUAGE MODEL FOR CONTROLLABLE GENERATION
判断是否二叉搜索树
Cadence OrCAD Capture TCL/TK脚本实例
VAD简单总结