线程池ThreadPoolExecutor并行处理实现代码
1、定义一个接口Animal
packagecom.zh.vo; publicinterfaceAnimal{ voidwork(); }
2、定义一个实现类Bird
packagecom.zh.vo; publicclassBirdimplementsAnimal{ @Override publicvoidwork(){ intsum=0; for(inti=0;i<100000;i++){ sum+=i; } System.out.println("Birdcalc:"+sum+"time:"+System.currentTimeMillis()); } }
3、定义一个实现类Cat
packagecom.zh.vo; publicclassCatimplementsAnimal{ @Override publicvoidwork(){ intsum=0; for(inti=0;i<100000;i++){ sum+=i; } System.out.println("Catcalc:"+sum+"time:"+System.currentTimeMillis()); } }
4、定义一个实现类Dog
packagecom.zh.vo; publicclassDogimplementsAnimal{ @Override publicvoidwork(){ intsum=0; for(inti=0;i<100000;i++){ sum+=i; } System.out.println("Dogcalc:"+sum+"time:"+System.currentTimeMillis()); } }
5、定义一个枚举类AnimalEnum
packagecom.zh.enums; importcom.zh.vo.Bird; importcom.zh.vo.Cat; importcom.zh.vo.Dog; publicenumAnimalEnum{ CAT("cat",Cat.class),DOG("dog",Dog.class),BIRD("bird",Bird.class); privateStringname; privateClass>clazz; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicClass>getClazz(){ returnclazz; } publicvoidsetClazz(Class>clazz){ this.clazz=clazz; } privateAnimalEnum(Stringname,Class>clazz){ this.name=name; this.clazz=clazz; } publicstaticvoidmain(String[]args){ //System.out.println(getName(DOG)); //System.out.println(getClazz(DOG)); AnimalEnum[]values=AnimalEnum.values(); System.out.println(values); } }
6、定义一个操作类AnimalUtil
packagecom.zh.utils; importjava.util.HashMap; importjava.util.Map; importcom.zh.enums.AnimalEnum; importcom.zh.vo.Animal; publicenumAnimalUtil{ INSTANCE; privatestaticMapmap=null; privatesynchronizedvoidinit(){ map=newHashMap (); AnimalEnum[]values=AnimalEnum.values(); for(AnimalEnumanimalEnum:values){ AnimalnewInstance=null; try{ newInstance=(Animal)animalEnum.getClazz().newInstance(); }catch(InstantiationExceptione){ e.printStackTrace(); }catch(IllegalAccessExceptione){ e.printStackTrace(); } map.put(animalEnum,newInstance); } } publicMap getEnumMaps(){ if(map==null||map.isEmpty()){ init(); } returnmap; } }
7、定义一个测试主类使用Future、Callable
packagecom.zh; importjava.util.HashMap; importjava.util.Map; importjava.util.Map.Entry; importjava.util.Set; importjava.util.concurrent.Callable; importjava.util.concurrent.ExecutionException; importjava.util.concurrent.Future; importjava.util.concurrent.LinkedBlockingQueue; importjava.util.concurrent.ThreadPoolExecutor; importjava.util.concurrent.TimeUnit; importcom.zh.enums.AnimalEnum; importcom.zh.utils.AnimalUtil; importcom.zh.vo.Animal; /** *@desc测试线程池 *@authorzhanhao */ publicclassThreadPoolSubmitTest{ /** *定义线程池 */ //Runtime.getRuntime().availableProcessors()*2 privatestaticThreadPoolExecutorthreadPoolExecutor= newThreadPoolExecutor(10,20,100,TimeUnit.MINUTES,newLinkedBlockingQueue()); /** *@desc执行主流程 *@paramargs *@throwsInterruptedException *@throwsExecutionException */ publicstaticvoidmain(String[]args)throwsInterruptedException,ExecutionException{ Map >futureMap=newHashMap >(); Set >entrySet=AnimalUtil.INSTANCE.getEnumMaps().entrySet(); for(Entry entry:entrySet){ futureMap.put(entry.getKey().getName(),exec(entry.getKey())); } for(Entry >entry:futureMap.entrySet()){ System.out.println(entry.getValue().get()); } waitForAllThreadFinish(); threadPoolExecutor.shutdown(); } /** *@desc讲任务提交到线程池中执行 *@paramenums *@return */ privatestaticFuture exec(AnimalEnumenums){ returnthreadPoolExecutor.submit(newCallable (){ @Override publicStringcall()throwsException{ Animalanimal=AnimalUtil.INSTANCE.getEnumMaps().get(enums); animal.work(); returnThread.currentThread().getName(); } }); } /** *@desc线程中有未完成的任务需等待完成 */ privatestaticvoidwaitForAllThreadFinish(){ while(threadPoolExecutor.getQueue().size()>0||threadPoolExecutor.getActiveCount()>0){ try{ Thread.sleep(10); }catch(InterruptedExceptione){ e.printStackTrace(); } } } }
8、执行结果
Dogcalc:704982704time:1574129306137
Birdcalc:704982704time:1574129306137
Catcalc:704982704time:1574129306137
pool-1-thread-1
pool-1-thread-2
pool-1-thread-3
9、定义一个测试主类使用Runnable
packagecom.zh; importjava.util.Map.Entry; importjava.util.Set; importjava.util.concurrent.ExecutionException; importjava.util.concurrent.LinkedBlockingQueue; importjava.util.concurrent.ThreadPoolExecutor; importjava.util.concurrent.TimeUnit; importcom.zh.enums.AnimalEnum; importcom.zh.utils.AnimalUtil; importcom.zh.vo.Animal; /** *@desc测试线程池 *@authorzhanhao */ publicclassThreadPoolExecuteTest{ /** *定义线程池 */ //Runtime.getRuntime().availableProcessors()*2 privatestaticThreadPoolExecutorthreadPoolExecutor= newThreadPoolExecutor(10,20,100,TimeUnit.MINUTES,newLinkedBlockingQueue()); /** *@desc执行主流程 *@paramargs *@throwsInterruptedException *@throwsExecutionException */ publicstaticvoidmain(String[]args)throwsInterruptedException,ExecutionException{ Set >entrySet=AnimalUtil.INSTANCE.getEnumMaps().entrySet(); for(Entry entry:entrySet){ exec(entry.getKey()); } waitForAllThreadFinish(); threadPoolExecutor.shutdown(); } /** *@desc讲任务提交到线程池中执行 *@paramenums *@return */ privatestaticvoidexec(AnimalEnumenums){ threadPoolExecutor.execute(newRunnable(){ @Override publicvoidrun(){ Animalanimal=AnimalUtil.INSTANCE.getEnumMaps().get(enums); animal.work(); System.out.println(Thread.currentThread().getName()); } }); } /** *@desc线程中有未完成的任务需等待完成 */ privatestaticvoidwaitForAllThreadFinish(){ while(threadPoolExecutor.getQueue().size()>0||threadPoolExecutor.getActiveCount()>0){ try{ Thread.sleep(10); }catch(InterruptedExceptione){ e.printStackTrace(); } } } }
10、执行结果
Birdcalc:704982704time:1574129356078
Dogcalc:704982704time:1574129356078
pool-1-thread-3
Catcalc:704982704time:1574129356078
pool-1-thread-1
pool-1-thread-2
注:1、submit方法可以有返回值2、submit底层调用execute方法
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。