线程WAITING--BlockingQueueDemo
生活随笔
收集整理的這篇文章主要介紹了
线程WAITING--BlockingQueueDemo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;/*** TODO 在此寫上類的相關說明.<br>* @author gongqiang <br>* @version 1.0.0 2021年6月2日<br>* @see * @since JDK 1.5.0*/
public class BlockingQueueDemo {/*** @param args*/public static void main(String[] args) {final BlockingQueue<Integer> blockQueue = new LinkedBlockingQueue<>(10);new Thread(() -> {while (true) {try {final Integer value = new Random().nextInt();final boolean success = blockQueue.offer(value);if (success) {System.out.println("向阻塞隊列添加" + value + "成功。");}Thread.sleep(7000);} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}).start();new Thread(() -> {while (true) {try {final Integer value = blockQueue.take();System.out.println("從阻塞隊列獲取" + value + "成功。");} catch (InterruptedException e) {Thread.currentThread().interrupt();}}}).start();try {Thread.sleep(60 * 60 * 1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
?
總結
以上是生活随笔為你收集整理的线程WAITING--BlockingQueueDemo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 所谓高情商就是会说话--总结
- 下一篇: 外设驱动库开发笔记30:宇电AI-BUS