线程池的理解

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;
}
@Override
public 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);
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值