????Java???Executors??????????????????
????newCachedThreadPool????????????????????????????????????????????????????????????????????????
????newFixedThreadPool ???????????????????????????????????????????????е????
????newScheduledThreadPool ???????????????????????????????????С?
????newSingleThreadExecutor ??????????????????????????????????????????????????????????????(FIFO?? LIFO?? ?????)??С?
????(1) newCachedThreadPool
??????????????????????????????????????????????????????????????????????????????????????£?
????packagetest; importjava.util.concurrent.ExecutorService; importjava.util.concurrent.Executors; publicclassThreadPoolExecutorTest{ publicstaticvoidmain(String[]args){ ExecutorServicecachedThreadPool=Executors.newCachedThreadPool(); for(inti=0;i<10;i++){ finalintindex=i; try{ Thread.sleep(index*1000); }catch(InterruptedExceptione){ e.printStackTrace(); } cachedThreadPool.execute(newRunnable(){ publicvoidrun(){ System.out.println(index); } }); } } }
??????????????????е???????????????????????????????е?????????????????????????????
????(2) newFixedThreadPool
???????????????????????????????????????????????????е??????????????£?
????packagetest; importjava.util.concurrent.ExecutorService; importjava.util.concurrent.Executors; publicclassThreadPoolExecutorTest{ publicstaticvoidmain(String[]args){ ExecutorServicefixedThreadPool=Executors.newFixedThreadPool(3); for(inti=0;i<10;i++){ finalintindex=i; fixedThreadPool.execute(newRunnable(){ publicvoidrun(){ try{ System.out.println(index); Thread.sleep(2000); }catch(InterruptedExceptione){ e.printStackTrace(); } } }); } } }
????????????С?3????????????index??sleep 2?????????????3???????
??????????????С?????????????????á???Runtime.getRuntime().availableProcessors()
????(3) newScheduledThreadPool
???????????????????????????????????????С?????????????????£?
????packagetest; importjava.util.concurrent.Executors; importjava.util.concurrent.ScheduledExecutorService; importjava.util.concurrent.TimeUnit; publicclassThreadPoolExecutorTest{ publicstaticvoidmain(String[]args){ ScheduledExecutorServicescheduledThreadPool=Executors.newScheduledThreadPool(5); scheduledThreadPool.schedule(newRunnable(){ publicvoidrun(){ System.out.println("delay3seconds"); } }??3??TimeUnit.SECONDS); } }
??????????3????С?
?????????????????????£?
????packagetest; importjava.util.concurrent.Executors; importjava.util.concurrent.ScheduledExecutorService; importjava.util.concurrent.TimeUnit; publicclassThreadPoolExecutorTest{ publicstaticvoidmain(String[]args){ ScheduledExecutorServicescheduledThreadPool=Executors.newScheduledThreadPool(5); scheduledThreadPool.scheduleAtFixedRate(newRunnable(){ publicvoidrun(){ System.out.println("delay1seconds??andexcuteevery3seconds"); } }??1??3??TimeUnit.SECONDS); } }
??????????1????3???????Ρ?
????(4) newSingleThreadExecutor
??????????????????????????????????????????????????????????????????(FIFO?? LIFO?? ?????)??С???????????£?
????packagetest; importjava.util.concurrent.ExecutorService; importjava.util.concurrent.Executors; publicclassThreadPoolExecutorTest{ publicstaticvoidmain(String[]args){ ExecutorServicesingleThreadExecutor=Executors.newSingleThreadExecutor(); for(inti=0;i<10;i++){ finalintindex=i; singleThreadExecutor.execute(newRunnable(){ publicvoidrun(){ try{ System.out.println(index); Thread.sleep(2000); }catch(InterruptedExceptione){ e.printStackTrace(); } } }); } } }
?????????????????????????и???????
????????????JDK???????????????????????????????????????????????????????????????????????
????????????C:Program FilesJavajdk1.6.0_06injconsole.exe
???????г????????????
????packagetest; importjava.util.concurrent.ExecutorService; importjava.util.concurrent.Executors; publicclassThreadPoolExecutorTest{ publicstaticvoidmain(String[]args){ ExecutorServicesingleThreadExecutor=Executors.newCachedThreadPool(); for(inti=0;i<100;i++){ finalintindex=i; singleThreadExecutor.execute(newRunnable(){ publicvoidrun(){ try{ while(true){ System.out.println(index); Thread.sleep(10*1000); } }catch(InterruptedExceptione){ e.printStackTrace(); } } }); try{ Thread.sleep(500); }catch(InterruptedExceptione){ e.printStackTrace(); } } } }
????Ч?????£?
??????????????е????
?????????????