????2.2.1.1 ???????????
????1???????ж????????????????????????????????????μ???????????????????????????????????
????2???????ж??????????????????????????????????洢?????????????????????????
????3???????ж???????????????????????У??????μ??????????????????????????????????
????ThreadPoolExecutor???execute()?????????
????1???????????е????????corePoolSize?????????????????????ò??????????????
????2????????е???????????corePoolSize???????????BlockingQueue;
????3???????????????????????????У??????μ???????????????ò????????????
????4???????????????????????е???????maximumPoolSize???????????????????RejectedExecutionHandle.rejectedExecution??????????
????2.2.2 ThreadPoolExecutor
????FixedThreadPool????????ù??????????????????????????????????????????????????ó????????????????????????
????public static ExecutorService newFixedThreadPool(int nThreads) {
????return new ThreadPoolExecutor(nThreads??nThreads??0L??TimeUnit.MILLISECONDS??new LinkedBlockingQueue<Runnable>());
????}
????SingleThreadPool?????????????????????????????и??????????????????????????ж????????????????????????????????
????public static ExecutorService newSingleThreadExecutor(){
????return new ThreadPoolExecutor(1??1??0L??TimeUnit.MILLISECONDS??new LinkedBlockingQueue<Runnable>());
????}
????CachedThreadPool???????????????????????????????????????????????????????п?????????????????п???????????????
????public static ExecutorService newCachedThreadPool(){
????return new ThreadPoolExecutor(0??Integer.Max_VALUE??60L??TimeUnit.SECONDS??new SysnchronousQueue<Runnable>());
????}????60??????????????
????2.2.3 ScheduledThreadPoolExecutor
????ScheduledThreadPoolExecutor???ThreadPoolExecutor?????????????????????????????????????????Timer???Timer??????????????ScheduledThreadPoolExecutor?????????????????????????????????
????ScheduledExecutorService????????2?????????????????????????
????scheduleAtFixedRate(Runnable command??long initialDelay??long period??TimeUnit unit)
??????????????÷????????????????????????????????????????????????????е???????????????period?1s??????????5s??????????????????????????????????С?
????????????????
????for (int i = 0; i < 3; i++) {
????Thread.sleep(1000);
????WorkerThread worker = new WorkerThread("do heavy processing");
????// schedule task to execute at fixed rate
????scheduledThreadPool.scheduleAtFixedRate(worker?? 0?? 10??
????TimeUnit.SECONDS);
???????
Current Time = Tue Oct 29 16:10:00 IST 2013
pool-1-thread-1 Start. Time = Tue Oct 29 16:10:01 IST 2013
pool-1-thread-2 Start. Time = Tue Oct 29 16:10:02 IST 2013
pool-1-thread-3 Start. Time = Tue Oct 29 16:10:03 IST 2013
pool-1-thread-1 End. Time = Tue Oct 29 16:10:06 IST 2013
pool-1-thread-2 End. Time = Tue Oct 29 16:10:07 IST 2013
pool-1-thread-3 End. Time = Tue Oct 29 16:10:08 IST 2013
pool-1-thread-1 Start. Time = Tue Oct 29 16:10:11 IST 2013
pool-1-thread-4 Start. Time = Tue Oct 29 16:10:12 IST 2013
????scheduleWithFixedDelay(Runnable command??long initialDelay??long delay??TimeUnit unit)
?????÷??????????????????????????delaytime?????????е?????ο?????????????????????????????
????for (int i = 0; i < 3; i++) {
????Thread.sleep(1000);
????WorkerThread worker = new WorkerThread("do heavy processing");
????scheduledThreadPool.scheduleWithFixedDelay(worker?? 0?? 1??
????TimeUnit.SECONDS);
????}
??????????
Current Time = Tue Oct 29 16:14:13 IST 2013
pool-1-thread-1 Start. Time = Tue Oct 29 16:14:14 IST 2013
pool-1-thread-2 Start. Time = Tue Oct 29 16:14:15 IST 2013
pool-1-thread-3 Start. Time = Tue Oct 29 16:14:16 IST 2013
pool-1-thread-1 End. Time = Tue Oct 29 16:14:19 IST 2013
pool-1-thread-2 End. Time = Tue Oct 29 16:14:20 IST 2013
pool-1-thread-1 Start. Time = Tue Oct 29 16:14:20 IST 2013
pool-1-thread-3 End. Time = Tue Oct 29 16:14:21 IST 2013
pool-1-thread-4 Start. Time = Tue Oct 29 16:14:21 IST 2013