JUC-java.util.concurrent学习(三)
BlockingQueue
Queue和List,Set是同一级的,Queue下面有阻塞队列 BlockingQueue,双端队列BlockingDeque,非阻塞队列AbstractQueue
阻塞:在队列为空拿去的时候会阻塞,在队列满了往里面放的时候会阻塞
四组API
操作 | 抛出异常 | 有返回值,不抛出异常 | 阻塞等待 | 超时等待 |
---|---|---|---|---|
添加 | add | offer | put | offer |
移除 | remove | poll | take | poll |
判断首 | element | peek |
/**
* 抛出异常
*/
public static void test1(){
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
//返回true或者false
System.out.println(blockingQueue.add("1"));
System.out.println(blockingQueue.add("2"));
System.out.println(blockingQueue.add("3"));
//java.lang.IllegalStateException: Queue full
//System.out.println(blockingQueue.add("3"));
//判断首位
System.out.println(blockingQueue.element());
System.out.println("-----------");
//qu去除的对象
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
System.out.println(blockingQueue.remove());
//java.util.NoSuchElementException
//System.out.println(blockingQueue.remove());
}
/**
* 有返回值,不抛出异常
*/
public static void test2(){
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("1"));
System.out.println(blockingQueue.offer("2"));
System.out.println(blockingQueue.offer("3"));
//false,不抛出异常
//System.out.println(blockingQueue.offer("4"));
//判断首位
System.out.println(blockingQueue.peek());
System.out.println("-----------");
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
//返回null
//System.out.println(blockingQueue.poll());
}
/**
* 阻塞等待
*/
public static void test3() throws InterruptedException {
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
blockingQueue.put("1");
blockingQueue.put("2");
blockingQueue.put("3");
//blockingQueue.put("1");//一直等待
//判断首位
System.out.println(blockingQueue.peek());
System.out.println("-----------");
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
System.out.println(blockingQueue.take());
//System.out.println(blockingQueue.take());//一直等待
}
/**
* 超时等待
*/
public static void test4() throws InterruptedException {
ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingQueue.offer("1"));
System.out.println(blockingQueue.offer("2"));
System.out.println(blockingQueue.offer("3"));
//超时等待
System.out.println(blockingQueue.offer("4",2, TimeUnit.SECONDS));
//判断首位
System.out.println(blockingQueue.peek());
System.out.println("-----------");
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
System.out.println(blockingQueue.poll());
//超时等待
System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));
}
SynchronousQueue
/**
* SynchronousQueue同步队列,不存储元素
* 放入一个值就必须要取出一个值
*/
public class synchronizedQueueDemo {
public static void main(String[] args) {
BlockingQueue<String> blockingQueue = new SynchronousQueue<String>();
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+"放入了第一个");
blockingQueue.put("1");
System.out.println(Thread.currentThread().getName()+"放入了第二个");
blockingQueue.put("2");
System.out.println(Thread.currentThread().getName()+"放入了第三个");
blockingQueue.put("3");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"T1").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(2);
System.out.println(Thread.currentThread().getName()+"->"+blockingQueue.take());
System.out.println(Thread.currentThread().getName()+"->"+blockingQueue.take());
System.out.println(Thread.currentThread().getName()+"->"+blockingQueue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
},"T2").start();
}
}