根据b站UP主狂神说JUC课程所写的个人学习笔记
视频地址:https://www.bilibili.com/video/BV1B7411L7tE?from=search&seid=14761503393031794075
阻塞
队列
阻塞队列:
BlockingQueue 不是新的东西
什么时候会使用阻塞队列:
多线程并发处理,线程池
学会使用队列
1.添加2.移除
四组api
1.抛出异常
2.不会抛出异常
3.阻塞等待
4.超时等待
| 方式 | 抛出异常 | 不会抛出异常,有返回值 | 阻塞等待 | 超时等待 |
| 添加 | add() | offer() | put() | offer(,,) |
| 移除 | remove() | poll() | take() | poll(,) |
| 判断队列首部 | element() | peek() |
public class BlockingQueueDemo {
public static void main(String[] args) {
//list
//set
//抛出异常
test1();
}
public static void test1(){
//队列的大小
ArrayBlockingQueue blockingqueue = new ArrayBlockingQueue<>(3);
System.out.println(blockingqueue.add("a"));
System.out.println(blockingqueue.add("b"));
System.out.println(blockingqueue.add("c"));
//java.lang.IllegalStateException: Queue full
//System.out.println(blockingqueue.add("d"));
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("a"));
System.out.println(blockingqueue.offer("b"));
System.out.println(blockingqueue.offer("c"));
//System.out.println(blockingqueue.offer("d"));//false不抛出异常
System.out.println(blockingqueue.poll());
System.out.println(blockingqueue.poll());
System.out.println(blockingqueue.poll());
System.out.println(blockingqueue.poll());//null不抛出异常
}
//阻塞(一直)
public static void test3() 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());//一直阻塞
}
//阻塞(等待超时)
public static void test4() throws InterruptedException {
ArrayBlockingQueue blockingqueue = new ArrayBlockingQueue<>(3);
blockingqueue.offer("a");
blockingqueue.offer("b");
blockingqueue.offer("c");
//blockingqueue.offer("d",2, TimeUnit.SECONDS);//等待超过2s就退出
System.out.println(blockingqueue.poll());
System.out.println(blockingqueue.poll());
System.out.println(blockingqueue.poll());
blockingqueue.poll(2,TimeUnit.SECONDS);
}
同步队列
SynchronousQueue
没有容量,进去一个元素,必须等待取出之后才能再往里面放一个元素
put,take
public class SynchronousQueueDemo {
public static void main(String[] args) {
SynchronousQueue <String> synchronousQueue = new SynchronousQueue<String>();//同步队列
new Thread(()->{
try {
System.out.println(Thread.currentThread().getName()+"put 1");
synchronousQueue.put("1");
System.out.println(Thread.currentThread().getName()+"put 2");
synchronousQueue.put("2");
System.out.println(Thread.currentThread().getName()+"put 3");
synchronousQueue.put("3");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t1").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"take"+synchronousQueue.take());
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"take"+synchronousQueue.take());
TimeUnit.SECONDS.sleep(3);
System.out.println(Thread.currentThread().getName()+"take"+synchronousQueue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
},"t2").start();
}
}