当前位置:Gxlcms > 数据库问题 > 六、阻塞队列LinkedBlockQueue

六、阻塞队列LinkedBlockQueue

时间:2021-07-01 10:21:17 帮助过:17人阅读

java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class BlockQueueDemo { private BlockingQueue<String> userQueue = new LinkedBlockingQueue<>(); private void offer() { // 每1秒钟,offer一条数据 for (int i = 0; i < 10; i++) { int id = i; new Thread(() -> { userQueue.offer("user: ID=" + id); }).start(); System.out.println("offered"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } private void poll() { // 每1秒钟,poll一条数据 for (int i = 0; i < 10; i++) { int id = i; new Thread(() -> { String user = userQueue.poll(); System.out.println(user); }).start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { BlockQueueDemo queueDemo = new BlockQueueDemo(); queueDemo.offer(); queueDemo.poll(); } }

 

六、阻塞队列LinkedBlockQueue

标签:dex   art   port   tst   选择   user   locking   try   耦合   

人气教程排行