线程池的理解

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 callable) {
                if (callable == null)
                    throw new NullPointerException();
                sync = new Sync(callable);
            }
     */
    final ArrayBlockingQueue queue = new ArrayBlockingQueue(2);
    TestQ t = new TestQ();

// t.testArrayQ(queue);
t.testPool(queue);
}

public void testPool(BlockingQueue queue){
    /**
     * public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue 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 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 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);
}
}

}

你可能感兴趣的:(java,心得)