Java 4??????????
???????????? ???????[ 2017/3/13 10:27:47 ] ??????????????????? Java
????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(); } } } }
????Ч?????£?
??????????????е????
?????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11