阻塞队列是一种特殊的队列,它具有线程安全的特性,并且在队列满或者空的情况下具有阻塞线程的能力。当队列为空时,试图从队列中获取元素的线程将被阻塞,直到队列中有可用元素;当队列已满时,试图向队列中添加元素的线程将被阻塞,直到队列中有空闲位置。
阻塞队列常用于生产者-消费者模式的多线程编程中,它可以很好地协调不同线程之间的数据共享和通信。Java 中的 java.util.concurrent 包提供了多种类型的阻塞队列实现,如 ArrayBlockingQueue、LinkedBlockingQueue 等,它们在多线程并发编程中发挥重要作用,简化了线程间的协作和数据传递。
package org.bq;
import java.util.concurrent.ArrayBlockingQueue;
public class Test {
//抛出异常
public static void main(String[] args) {
//队列的大小
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.add("a"));
System.out.println(blockingQueue.add("b"));
System.out.println(blockingQueue.add("c"));
System.out.println(blockingQueue.add("d"));
}
}
package org.bq;
import java.util.concurrent.ArrayBlockingQueue;
public class Test {
//抛出异常
public static void main(String[] args) {
//队列的大小
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.add("a"));
System.out.println(blockingQueue.add("b"));
System.out.println(blockingQueue.add("c"));
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
}
}
package org.bq;
import java.util.concurrent.ArrayBlockingQueue;
public class Test1 {
public static void main(String[] args) {
test2();
}
public static void test1(){
}
public static void test2(){
//队列的大小
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
System.out.println(blockingQueue.offer("d")); //false 不抛出异常
}
}
package org.bq;
import java.util.concurrent.ArrayBlockingQueue;
public class Test1 {
public static void main(String[] args) {
test2();
}
public static void test1(){
}
public static void test2(){
//队列的大小
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("a"));
System.out.println(blockingQueue.offer("b"));
System.out.println(blockingQueue.offer("c"));
//System.out.println(blockingQueue.offer("d")); //false 不抛出异常
System.out.println("===============");
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll()); //null 不抛出异常
}
}
package org.Test6;
import java.util.concurrent.ArrayBlockingQueue;
public class Test2 {
public static void main(String[] args) throws InterruptedException {
//队列的大小
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
//一直阻塞
blockingQueue.put("a");
blockingQueue.put("b");
blockingQueue.put("c");
// blockingQueue.put("d");//阻塞
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take()); //阻塞,会一直等待
}
}
package org.Test6;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
public class Test3 {
public static void main(String[] args) throws InterruptedException {
//队列的大小
ArrayBlockingQueue blockingQueue=new ArrayBlockingQueue<>(3);
blockingQueue.offer("1");
blockingQueue.offer("2");
blockingQueue.offer("3");
// blockingQueue.offer("4",2, TimeUnit.SECONDS); //等待超过2S后退出
blockingQueue.poll();
blockingQueue.poll();
blockingQueue.poll();
blockingQueue.poll(2,TimeUnit.SECONDS); //等待2S后退出
}
}
在技术的道路上,我们不断探索、不断前行,不断面对挑战、不断突破自我。科技的发展改变着世界,而我们作为技术人员,也在这个过程中书写着自己的篇章。让我们携手并进,共同努力,开创美好的未来!愿我们在科技的征途上不断奋进,创造出更加美好、更加智能的明天!