JUC练习8——队列
一,java中队列实现的位置(在多线程并发处理,和线程池计算中使用到并发技术)
二,阻塞队列的基本操作:添加,移除元素
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import org.junit.Test; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; public class JUCTest { /** * add 添加后超过容量抛异常,否则返回true * remove 队列中没有元素抛异常,否则返回元素 */ @Test public void test1() { ArrayBlockingQueue<String> blockingQueue1 = new ArrayBlockingQueue<>( 2 ); System.out.println(blockingQueue1.add( "a" )); System.out.println(blockingQueue1.add( "b" )); // blockingQueue1.add("C"); System.out.println(blockingQueue1.remove()); System.out.println(blockingQueue1.remove()); System.out.println(blockingQueue1.remove()); } /** * offer 添加后超过容量返回false * remove 队列中没有元素返回null */ @Test public void test2() { ArrayBlockingQueue<String> blockingQueue1 = new ArrayBlockingQueue<>( 2 ); System.out.println(blockingQueue1.offer( "a" )); System.out.println(blockingQueue1.offer( "b" )); System.out.println(blockingQueue1.offer( "C" )); System.out.println(blockingQueue1.poll()); System.out.println(blockingQueue1.poll()); System.out.println(blockingQueue1.poll()); } /** * put 添加后超过容量,线程会一直等待队列有位置时,将元素插入队列 * take 队列中没有元素,一直等待元素的取出 */ @Test public void test3() throws InterruptedException { ArrayBlockingQueue<String> blockingQueue1 = new ArrayBlockingQueue<>( 2 ); blockingQueue1.put( "A" ); blockingQueue1.put( "B" ); // blockingQueue1.put("C"); System.out.println(blockingQueue1.take()); System.out.println(blockingQueue1.take()); System.out.println(blockingQueue1.take()); } /** * 超时等到,如果两秒中元素的添加和移除还没有满足添加就跳过 * put 添加后超过容量,线程会一直等待队列有位置时,将元素插入队列 * take 队列中没有元素,一直等待元素的取出 */ @Test public void test4() throws InterruptedException { ArrayBlockingQueue<String> blockingQueue1 = new ArrayBlockingQueue<>( 2 ); blockingQueue1.offer( "A" ); blockingQueue1.offer( "B" ); blockingQueue1.offer( "C" , 2 , TimeUnit.SECONDS); System.out.println(blockingQueue1.poll()); System.out.println(blockingQueue1.poll()); System.out.println(blockingQueue1.poll( 2 ,TimeUnit.SECONDS)); } } |
三,同步队列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | /** * 同步队列,不存储元素 * 只有先存入一个元素后才能取元素,如果元素没有被取出,就不能存入元素,线程等待 * 使用for循环,有点问题 */ public static void main(String[] args) { test5(); } //@Test public static void test5() { BlockingQueue<Integer> synchronousQueue = new SynchronousQueue<>(); new Thread(()->{ try { synchronousQueue.put( 1 ); System.out.println(Thread.currentThread().getName()+ " put " + 1 ); synchronousQueue.put( 2 ); System.out.println(Thread.currentThread().getName()+ " put " + 2 ); synchronousQueue.put( 3 ); System.out.println(Thread.currentThread().getName()+ " put " + 3 ); } catch (InterruptedException e) { e.printStackTrace(); } }, "A" ).start(); new Thread(()->{ try { TimeUnit.SECONDS.sleep( 2 ); System.out.println(Thread.currentThread().getName()+ " get " +synchronousQueue.take()); TimeUnit.SECONDS.sleep( 2 ); System.out.println(Thread.currentThread().getName()+ " get " +synchronousQueue.take()); TimeUnit.SECONDS.sleep( 2 ); System.out.println(Thread.currentThread().getName()+ " get " +synchronousQueue.take()); } catch (InterruptedException e) { e.printStackTrace(); } }, "B" ).start(); } |
执行结果:
A put 1
B get 1
A put 2
B get 2
A put 3
B get 3
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现