创立线程池时
不主张直接运用履行者进行创立线程池缓存线程池或许创立过多线程行列过大,形成内存溢出不同线程池之间的创立
@ SLF 4j公共课房池{/* * *不限数量创立线程,最多创立Integer.max个线程*/ExecutorService cachedThreadPool=executors。newcachedthreadpool();/** * 创立固定数量线程池*/ExecutorService fixedThreadPool=executors。newfixedthreadpool(5);/** * 单线程池*/ExecutorService singleThread=executors。newsinglethreadexecutor();/** * 守时使命线程池*/ScheduledExecutorService scheduledThreadPool=executors。newscheduledthreadpool(5);/** * 自定义线程池* 1、中心线程数-理论上讲用户线程数能够无限多,首要受机器内存影响* 2、最大线程数* 3、超越中心线程的闲暇存活时刻* 4、时刻单位* 5、堵塞行列* LinkedBlockingQueue -假如不限定数量则是一个无界行列,或许会引起内存溢出(OOM:OUT内存不足)* ArrayBlockingQueue -有界行列*优先级堵塞行列-有界优先行列*同步行列-只能寄存一个元素* 6、线程工厂* 7、回绝战略* ThreadPoolExecutor .堕胎方针*线程池履行程序.调用者运转战略*线程池履行器.DiscardOldestPolicy *线程池履行器.discard policy */ExecutorService my self horse pool=new thread pool executor(5,10,10,TimeUnit .毫秒,new LinkedBlockingQueueRunnable(100),new ThreadFactory(){ security manager s=system。getsecuritymanager();私有终究线程组group=(s!=null)?s . getthreadgroup():线程。当时线程().getThreadGroup();私有终究原子整数线程数=新原子整数(1);private final String threadname prefix=' my self horse pool-horse-';@掩盖公共线程新线程(Runnable r){ Thread Thread=新线程(group,r,threadname前缀线程号。getandincrement(),0);回来线程;} },新建ThreadPoolExecutor .DiscardOldestPolicy());/** * 有多少个使命,则一次创立多少个线程* 从日志能够看出一共创立了10个线程*这些池通常会进步履行许多短期异步使命的程序的功能*/public vo useCacheThreadPool(){ log。info('-{ }-{ }-',' useCacheThreadPool ',' start ');horse runnable horse runnable=new horse runnable();for(int I=0;i 10i ) { c achedThreadPool.submit(horseRunnable); } cachedThreadPool.shutdown(); } /** * 每次只发动固定数量的线程 * 假如没有线程则堵塞等候 */ public vo useFixedThreadPool(){ log.info("-----------------{}---{}-------------------------","useFixedThreadPool","start"); HorseRunnable horseRunnable = new HorseRunnable(); for (int i = 0; i < 10; i ) { fixedThreadPool.submit(horseRunnable); } fixedThreadPool.shutdown(); } /** * 每次只发动一个线程履行使命 */ public vo useSingleThread(){ log.info("-----------------{}---{}-------------------------","useSingleThread","start"); HorseRunnable horseRunnable = new HorseRunnable(); for (int i = 0; i < 10; i ) { singleThread.submit(horseRunnable); } singleThread.shutdown(); } /** * 守时使命 * 初始推迟1s,后期每次推迟3s */ public vo useScheduledThreadPool(){ log.info("-----------------{}---{}-------------------------","useScheduledThreadPool","start"); HorseRunnable horseRunnable = new HorseRunnable(); scheduledThreadPool.scheduleAtFixedRate(horseRunnable,1,3,TimeUnit.MILLISECONDS); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); }finally { scheduledThreadPool.shutdown(); } } /** * 发动自定义线程池 */ public vo useMyselfHorsePool(){ log.info("-----------------{}---{}-------------------------","useMyselfHorsePool","start"); HorseRunnable horseRunnable = new HorseRunnable(); for (int i = 0; i < 10; i ) { myselfHorsePool.submit(horseRunnable); } myselfHorsePool.shutdown(); } public vo useHorsePoolCallable() throws ExecutionException, InterruptedException { ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); log.info("-----------------{}---{}-------------------------","useMySelfHorsePoolCallable","start"); HorseCallable horseCallable = new HorseCallable(); List<Future> resultList = new ArrayList<>(); for (int i = 0; i < 10; i ) { Future resultFuture = cachedThreadPool.submit(horseCallable); resultList.add(resultFuture); } for (Future future : resultList) { Object callable = future.get(); log.info("callable:{}",callable); } cachedThreadPool.shutdown(); } public vo runRunnable(HousePool housePool){ int number =0; try { Thread.sleep(1000); housePool.useCacheThreadPool(); /* 经过循环调用来检查前一个线程池是否现已履行结束 */ while(true){ if(housePool.cachedThreadPool.isTerminated()&&number==0) { housePool.useSingleThread(); number ; Thread.sleep(2000); }else if(housePool.singleThread.isTerminated()&&number==1) { housePool.useScheduledThreadPool(); number ; Thread.sleep(2000); }else if(housePool.scheduledThreadPool.isTerminated()&&number==2) { housePool.useFixedThreadPool(); number ; Thread.sleep(2000); }else if(housePool.fixedThreadPool.isTerminated()&&number==3) { housePool.useMyselfHorsePool(); number ; Thread.sleep(2000); }else if(housePool.myselfHorsePool.isTerminated()) { break; } } } catch (InterruptedException e) { e.printStackTrace(); } } public vo runCallable(HousePool housePool){ try { housePool.useHorsePoolCallable(); } catch (ExecutionException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 履行不同类型线程池的submit办法 * 一个线程池能够一起履行完成runnable接口的办法和完成callable接口的办法 * @param args */ public static vo main(String<> args) { HousePool housePool =new HousePool(); housePool.runRunnable(housePool); housePool.runCallable(housePool); }}四种回绝战略
* 7、回绝战略 直接扔掉,并报反常 * ThreadPoolExecutor.AbortPolicy调用线程来履行使命 * ThreadPoolExecutor.CallerRunsPolicy扔掉最老的使命 * ThreadPoolExecutor.DiscardOldestPolicy直接扔掉新来的使命 * ThreadPoolExecutor.DiscardPolicy
以上便是小编为我们带来的全部内容,期望能够协助到我们。