线程池

public class ThreadPoolDemo01 {
public static void main(String[] args) {
/*
public ThreadPoolExecutor(
                  int corePoolSize,//参数一:指定线程池的线程数量(核心线程): corePoolSize
int maximumPoolSize,//参数二:指定线程池可支持的最大线程数: maximumPoolSize
long keepAliveTime,//参数三:指定临时线程的最大存活时间: keepAliveTime
TimeUnit unit,//参数四:指定存活时间的单位(秒、分、时、天): unit
BlockingQueue<Runnable> workQueue,//参数五:指定任务队列: workQueue
ThreadFactory threadFactory,//参数六:指定用哪个线程工厂创建线程: threadFactory
RejectedExecutionHandler handler)//参数七:指定线程忙,任务满的时候,新任务来了怎么办: handler
*/
ExecutorService pool = new ThreadPoolExecutor(3,5,6,
TimeUnit.SECONDS,new ArrayBlockingQueue<>(5),
Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());

//2. 给任务线程池处理
Runnable target = new MyRunnable();
//new Thread(myThread).start();
pool.execute(target);
pool.execute(target);
pool.execute(target);
pool.execute(target);

}

}
class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++){
System.out.println(Thread.currentThread().getName()+"执行 "+i);
}
}
}

posted on 2022-03-30 23:57  我要当程序源  阅读(15)  评论(0编辑  收藏  举报

导航