阻塞队列 BlockingQueue
方式 | 抛出异常 | 有返回值 | 阻塞等待 | 超时等待 |
---|---|---|---|---|
添加 | add | offer | put | offer(,) |
移除 | remove | poll | take | poll(,) |
判断队列首 | elment |
- 抛出异常 出现异常直接抛出
public static void test1(){
//初始化是写入最大容量
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
System.out.println(arrayBlockingQueue.add("a"));
System.out.println(arrayBlockingQueue.add("b"));
System.out.println(arrayBlockingQueue.add("c"));
//System.out.println(arrayBlockingQueue.add("d"));
System.out.println(arrayBlockingQueue.remove());
System.out.println(arrayBlockingQueue.remove());
System.out.println(arrayBlockingQueue.remove());
}
- 有返回值 不会抛出异常
public static void test2(){
//初始化是写入最大容量
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
System.out.println(arrayBlockingQueue.offer("a"));
System.out.println(arrayBlockingQueue.offer("b"));
System.out.println(arrayBlockingQueue.offer("c"));
//System.out.println(arrayBlockingQueue.add("d"));
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
}
- 阻塞等待
public static void test3() throws InterruptedException {
//初始化是写入最大容量
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
arrayBlockingQueue.put("a");
arrayBlockingQueue.put("b");
arrayBlockingQueue.put("c");
//System.out.println(arrayBlockingQueue.add("d"));
System.out.println(arrayBlockingQueue.take());
System.out.println(arrayBlockingQueue.take());
System.out.println(arrayBlockingQueue.take());
System.out.println(arrayBlockingQueue.take());
}
- 超时等待 超过设置的时间就会取消等待
public static void test4() throws InterruptedException {
//初始化是写入最大容量
ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
System.out.println(arrayBlockingQueue.offer("a"));
System.out.println(arrayBlockingQueue.offer("b"));
System.out.println(arrayBlockingQueue.offer("c"));
//System.out.println(arrayBlockingQueue.add("d"));
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll(2, TimeUnit.SECONDS));
}
同步队列 SynchronousQueue
- 没有容量 只能放进去一个元素取出后才可以放入
public static void main(String[] args) {
SynchronousQueue<String> synchronousQueue = new SynchronousQueue<>();
new Thread(()->{
try {
System.out.println("1111put");
synchronousQueue.put("11");
System.out.println("2222put");
synchronousQueue.put("22");
System.out.println("3333put");
synchronousQueue.put("33");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"1111").start();
new Thread(()->{
try {
TimeUnit.SECONDS.sleep(2);
synchronousQueue.take();
System.out.println(111);
TimeUnit.SECONDS.sleep(2);
synchronousQueue.take();
System.out.println(222);
TimeUnit.SECONDS.sleep(2);
synchronousQueue.take();
System.out.println(333);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
线程池
-
降低资源的消耗 提高响应的速度(不用新建和销毁)
-
方便管理
public ThreadPoolExecutor(int corePoolSize,//核心线程数
int maximumPoolSize,//最大核心线程数
long keepAliveTime,//超时时间
TimeUnit unit,//超时时间单位
BlockingQueue<Runnable> workQueue,//阻塞队列
ThreadFactory threadFactory,
//线程工厂,创建线程的
RejectedExecutionHandler handler//拒绝策略
) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}
线程池的创建
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
2,
5,
3,
TimeUnit.SECONDS,
new LinkedBlockingDeque<>(3),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy());
}
线程池的四种拒绝策略
最大线程如何定义
- CPU密集型
- 几核CPU就定义为几可以保持CPU的效率最高
//获取CPU的核数
Runtime.getRuntime().availableProcessors();
- IO密集型
- 判断程序中十分消耗IO的线程