标签:rgs rate 资源 抛出异常 拒绝策略 简单的 time name .sh
示例:
public class CustomThreadPoolExecutor {
//private static volatile ThreadPoolExecutor pool= null;
private static volatile ThreadPoolExecutor pool =null;
public static ThreadPoolExecutor getInstance(){
if(pool ==null){
synchronized (ThreadPoolExecutor.class) {
if(pool == null){
init();
return pool;
}
}
}
return pool;
}
private static void init() {
// TODO Auto-generated method stub
pool=new ThreadPoolExecutor(10, 30, 30, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>(),
new CustomThreadFactory(),new CustomRejectedExecutionHandler());
}
private static void destory(){
if(pool != null){
pool.shutdown();
}
}
private static class CustomThreadFactory implements ThreadFactory{
private AtomicInteger count=new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
// TODO Auto-generated method stub
Thread t = new Thread(r);
String threadName = CustomThreadPoolExecutor.class.getSimpleName()+count.addAndGet(1);
System.out.println(threadName);
t.setName(threadName);
return t;
}
}
private static class CustomRejectedExecutionHandler implements RejectedExecutionHandler{
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// TODO Auto-generated method stub
//拒绝策略
}
}
public static void main(String[] args) {
ThreadPoolExecutor pool=CustomThreadPoolExecutor.getInstance();
for(int i=1;i<100;i++){
System.out.println("提交第"+i+"个任务!");
pool.execute(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("InterruptedException1");
}
System.out.println("running-----"+Thread.currentThread().getId());
}
});
System.out.println("pool size"+pool.getPoolSize());
}
}
}
拒绝策略:
标签:rgs rate 资源 抛出异常 拒绝策略 简单的 time name .sh
原文地址:https://www.cnblogs.com/jing-ma/p/9662507.html