Java线程池的几种实现方法和区别介绍实例详解

  

<强>下面通过实例代码为大家介绍Java线程池的几种实现方法和区别:
  

        进口java.text.DateFormat;   进口java.text.SimpleDateFormat;   进口java.util.ArrayList;   进口java.util.Date;   进口并不知道;   进口java.util.Random;   进口java.util.concurrent.Callable;   进口java.util.concurrent.ExecutorService;   进口java.util.concurrent.Executors;   进口java.util.concurrent.Future;   公开课TestThreadPool {//-newFixedThreadPool与cacheThreadPool差不多,也是能重用就用,但不能随时建新的线程//涠捞刂?任意时间点,最多只能有固定数目的活动线程存在,此时如果有新的线程要建立,只能放在另外的队列中等待,直到当前的线程中某个线程终止直接被移出池子//蚦acheThreadPool不同,FixedThreadPool没有空闲机制(可能也有,但既然文档没提,肯定非常长,类似依赖上层的TCP或UDP//空闲机制之类的),所以FixedThreadPool多数针对一些很稳定很固定的正规并发线程,多用于服务器//- - - - - -从方法的源代码看,缓存池和固定池调用的是同一个底层池,只不过参数不同://固定池线程数固定,并且是0秒闲置(无闲置)//缓存池线程数支持0-Integer.MAX_VALUE(显然完全没考虑主机的资源承受能力),60秒闲置   私有静态ExecutorService fixedService=Executors.newFixedThreadPool (6);//-缓存型池子,先查看池中有没有以前建立的线程,如果有,就重用。如果没有,就建一个新的线程加入池中//-缓存型池子通常用于执行一些生存期很短的异步型任务//因此在一些面向连接的守护进程型服务器中用得不多。//苤赜玫南叱?必须是空闲超时内的池中线程,缺省超时是60年代,超过这个空闲时长,线程实例将被终止及移出池。//注意,放入CachedThreadPool的线程不必担心其结束,超过超时不活动,其会自动被终止。   私有静态ExecutorService cacheService=Executors.newCachedThreadPool ();//ダ叱?任意时间池中只能有一个线程//玫氖呛突捍娉睾凸潭ǔ叵嗤牡撞愠?但线程数目是1 - 1,0秒闲置(无闲置)   私有静态ExecutorService singleService=Executors.newSingleThreadExecutor ();//鞫刃拖叱坛?/- - - - - -这个池子里的线程可以按时间表依次延迟执行,或周期执行   私有静态ExecutorService scheduledService=Executors.newScheduledThreadPool (10);   公共静态void main (String [] args) {   DateFormat格式=new SimpleDateFormat (“yyyy-MM-dd HH: mm: ss”);   ListcustomerList=new ArrayList ();   System.out.println(格式。新日期格式(()));   testFixedThreadPool (fixedService customerList);   System.out.println (“- - - - - - - - - - - - - - - - - - - - - - - - - - - -”);   testFixedThreadPool (fixedService customerList);   fixedService.shutdown ();   System.out.println (fixedService.isShutdown ());   System.out.println (“- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -”);   testCacheThreadPool (cacheService customerList);   System.out.println (“- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -”);   testCacheThreadPool (cacheService customerList);   cacheService.shutdownNow ();   System.out.println (“- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -”);   testSingleServiceThreadPool (singleService customerList);   testSingleServiceThreadPool (singleService customerList);   singleService.shutdown ();   System.out.println (“- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -”);   testScheduledServiceThreadPool (scheduledService customerList);   testScheduledServiceThreadPool (scheduledService customerList);   scheduledService.shutdown ();   }   公共静态孔隙testScheduledServiceThreadPool (ExecutorService服务,ListcustomerList) {   List比;listCallable=new ArrayList在();   for (int i=0;我& lt;10;我+ +){   Callable可调用=new Callable () {   @Override   公共整数调用()抛出异常{   随机()返回新.nextInt (10);   }   };   listCallable.add(调用);   }   尝试{   List比;listFuture=service.invokeAll (listCallable);   (Future未来:listFuture) {   整数id=future.get ();   customerList.add (id);   }   }捕捉(异常e) {   e.printStackTrace ();   }   System.out.println (customerList.toString ());   }   公共静态孔隙testSingleServiceThreadPool (ExecutorService服务,List

Java线程池的几种实现方法和区别介绍实例详解