第一组
add()方法添加数据
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestDemo5 {
public static void main(String[] args) {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
for (int i = 0; i <6 ; i++) {
blockingQueue.add(i);
}
}
}
运行结果:
可以看到这个add方法,在我们添加的时候,如果队列已经满了,就会抛出队列已满异常。
remove()方法弹出数据
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestDemo5 {
public static void main(String[] args) {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
blockingQueue.add(1);
blockingQueue.add(1);
blockingQueue.add(1);
blockingQueue.add(1);
blockingQueue.add(1);
for (int i = 0; i <6 ; i++) {
blockingQueue.remove();
}
}
}
运行结果:
这个remove方法和add配套使用,弹出队列的元素,当队列里面没有元素了也会抛出异常
第二种
offer()方法添加数据
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestDemo5 {
public static void main(String[] args) {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
}
}
运行结果:
可以看到,这种往队列添加数据的offer方法,当队列满了的时候也不会抛出异常。
poll()方法弹出数据
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestDemo5 {
public static void main(String[] args) {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
System.out.println(blockingQueue.offer(1));
for (int i = 0; i <6 ; i++) {
System.out.println(blockingQueue.poll());
}
}
}
运行结果:
当队列没有数据的时候会返回一个null
第三种
put()方法添加数据
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestDemo5 {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
}
}
运行结果:
就是正常的添加数据,现在我们再往队列种添加一个数据,然后查看一下结果:
程序不会结束,会一直等,也就是说这种添加数据的方法,当队列满的时候,会一直死等。
take()方法弹出数据
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestDemo5 {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
for (int i = 0; i <5 ; i++) {
System.out.println(blockingQueue.take());
}
}
}
结果:
现在我们增加一次循环的次数看一下结果是不是也会一直等待:
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TestDemo5 {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
blockingQueue.put(1);
for (int i = 0; i <6 ; i++) {
System.out.println(blockingQueue.take());
}
}
}
结果:
和我们预期的一样,当队列没有数据的时候,会一直等到有数据,然后再输出数据。
第四种
offer()方法超时等待
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class TestDemo5 {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,3, TimeUnit.SECONDS);
}
}
运行结果:
当队列满的时候,程序会先等待3秒钟,然后程序停止。这种方式避免了一直等待,占用cpu资源。
poll()方法
package com.qcby.algorithm;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
public class TestDemo5 {
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<Integer>(5);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
blockingQueue.offer(1,2, TimeUnit.SECONDS);
for (int i = 0; i <6 ; i++) {
System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS));
}
}
}
运行结果:
最后一个null不是一下就输出的,而是在输出队列数据的时候,发现没有数据了,然后等了两秒还没有数据,然后输出null。
以上就是我对阻塞队列四种API的简单了解。希望小伙伴们自己手动练习一下,加深印象。