线程池的理解
来源:互联网 发布:文明6 mac版 编辑:程序博客网 时间:2024/05/19 18:12
package com.test.thread;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestQ {
public static void main(String[] args) throws InterruptedException { /** * LinkedBlockingQueue 链表队列 Node head,tail; * ArrayBlockingQueue 有界队列 * PriorityBlockingQueue 优先级队列 先获取优先级别高的 * SynchronousQueue 其中每个 put 必须等待一个 take,反之亦然。同步队列没有任何内部容量,甚至连一个队列的容量都没有 * * * 工作线程 * RejectedExecutionHandler //如果不能再放了 可以放到handler中的 * 四种策略 addWorker 失败时候 * AbortPolicy //抛出异常 * CallerRunsPolicy //执行run * DiscardOldestPolicy //会继续丢进队列中,可能导致stack溢出 需要注意 * DiscardPolicy //不做任何处理的不执行 * * * ExecutorService service = Executors.newCachedThreadPool(); service.submit(); //submit提交的任何最后都会被转换为callable //runnable通过封装后转换为callable public FutureTask(Runnable runnable, V result) { sync = new Sync(Executors.callable(runnable, result)); //通过RunnableAdapter 适配器模式 } public FutureTask(Callable<V> callable) { if (callable == null) throw new NullPointerException(); sync = new Sync(callable); } */ final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(2); TestQ t = new TestQ();
// t.testArrayQ(queue);
t.testPool(queue);
}
public void testPool(BlockingQueue<Runnable> queue){ /** * public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler);} */ RejectedEHandler handle = new RejectedEHandler(); ThreadPoolExecutor executor = new ThreadPoolExecutor(10,10,10L,TimeUnit.SECONDS,queue,handle); for (int i = 0; i < 100; i++) { executor.execute(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getId()+"---running"); } }); }}public void testArrayQ(BlockingQueue<String> queue){ for (int j = 0; j < 10000; j++) { Put put = new Put(queue,new Random().nextInt(10000)+""); new Thread(put).start(); } for (int j = 0; j < 500; j++) { Take take = new Take(queue); new Thread(take).start(); } }}
class Take implements Runnable{
private BlockingQueue queue;
public Take(BlockingQueue queue){
this.queue = queue;
}
@Override
public void run() {
try {
System.out.println(“take:”+queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Put implements Runnable{
private BlockingQueue queue;
private String msg;
public Put(BlockingQueue<String> queue,String msg){ this.queue = queue; this.msg = msg;}@Overridepublic void run() { try { queue.put(msg); System.out.println("put msg:"+msg); } catch (InterruptedException e) { e.printStackTrace(); }}
}
class RejectedEHandler implements RejectedExecutionHandler{
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println(“handler running—>”+Thread.currentThread().getId());
if(!executor.isShutdown()){
executor.execute(r);
}
}
}
- 线程池的理解
- 线程池的理解
- 线程池的理解
- 线程池的理解
- 线程池的理解
- 关于线程池的理解
- android线程池的理解
- 理解线程池的笔记
- 对线程池的理解
- JAVA 线程池的理解
- 理解线程池的原理
- 线程池机制的理解
- 理解线程池的原理
- 关于线程池的理解
- java线程池的理解
- 理解线程池的原理
- 理解线程池的原理
- JAVA线程池的理解
- Android工程师面试题大全
- ROKID桌面机器人麦克风阵列拆解分析及设计建议
- 分区
- 剑指offer 35题 【时间空间效率的平衡】第一个只出现一次的字符
- NGUI根据图片是否透明来决定是否可以点中
- 线程池的理解
- 飞凌Fl2440开发板蜂鸣器pwm的驱动
- 解决font-face跨域办法
- 2016 Multi-University Training Contest 2(2016多校训练第二场)1009
- new、delete与malloc、free
- clauch svn使用设置
- androidStudio没有httpclient的解决办法
- iOS 如何获取应用当前的控制器 UIWindow & UIWindowLevel笔记
- 数据库sql 查询列属性 增加列 修改列长度 建表 自增