您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 商业计划书 > java中通用的线程池实例代码
java中通用的线程池实例代码java中通用的线程池实例代码,需要的朋友可以参考一下代码如下:packagecom.smart.frame.task.autoTask;importjava.util.Collection;importjava.util.Vector;/***任务分发器*/publicclassTaskManageextendsThread{protectedVectorRunnabletasks=newVectorRunnable();protectedbooleanrunning=false;protectedbooleanstopped=false;protectedbooleanpaused=false;protectedbooleankilled=false;privateThreadPoolpool;publicTaskManage(ThreadPoolpool){this.pool=pool;}publicvoidputTask(Runnabletask){tasks.add(task);}publicvoidputTasks(Runnable[]tasks){for(inti=0;itasks.length;i++)this.tasks.add(tasks[i]);}publicvoidputTasks(CollectionRunnabletasks){this.tasks.addAll(tasks);}protectedRunnablepopTask(){if(tasks.size()0)return(Runnable)tasks.remove(0);elsereturnnull;}publicbooleanisRunning(){returnrunning;}publicvoidstopTasks(){stopped=true;}publicvoidstopTasksSync(){stopTasks();while(isRunning()){try{sleep(5);}catch(InterruptedExceptione){TaskException.getResultMessage(e);}}}publicvoidpauseTasks(){paused=true;}publicvoidpauseTasksSync(){pauseTasks();while(isRunning()){try{sleep(5);}catch(InterruptedExceptione){TaskException.getResultMessage(e);}}}publicvoidkill(){if(!running)interrupt();elsekilled=true;}publicvoidkillSync(){kill();while(isAlive()){try{sleep(5);}catch(InterruptedExceptione){TaskException.getResultMessage(e);}}}publicsynchronizedvoidstartTasks(){running=true;this.notify();}publicsynchronizedvoidrun(){try{while(true){if(!running||tasks.size()==0){pool.notifyForIdleThread();this.wait();}else{Runnabletask;while((task=popTask())!=null){task.run();if(stopped){stopped=false;if(tasks.size()0){tasks.clear();System.out.println(Thread.currentThread().getId()+:Tasksarestopped);break;}}if(paused){paused=false;if(tasks.size()0){System.out.println(Thread.currentThread().getId()+:Tasksarepaused);break;}}}running=false;}if(killed){killed=false;break;}}}catch(InterruptedExceptione){TaskException.getResultMessage(e);return;}}}复制代码代码如下:packagecom.smart.***线程池*/publicclassThreadPool{protectedintmaxPoolSize=TaskConfig.maxPoolSize;protectedintinitPoolSize=TaskConfig.initPoolSize;protectedVectorTaskManagethreads=newVectorTaskManage();protectedbooleaninitialized=false;protectedbooleanhasIdleThread=false;publicThreadPool(){super();}publicThreadPool(intmaxPoolSize,intinitPoolSize){this.maxPoolSize=maxPoolSize;this.initPoolSize=initPoolSize;}publicvoidinit(){initialized=true;for(inti=0;iinitPoolSize;i++){TaskManagethread=newTaskManage(this);thread.start();threads.add(thread);}}publicvoidsetMaxPoolSize(intmaxPoolSize){this.maxPoolSize=maxPoolSize;if(maxPoolSizegetPoolSize())setPoolSize(maxPoolSize);}/***重设当前线程数若需杀掉某线程,线程不会立刻杀掉,而会等到线程中的事*务处理完成但此方法会立刻从线程池中移除该线程,不会等待事务处理结束*/publicvoidsetPoolSize(intsize){if(!initialized){initPoolSize=size;return;}elseif(sizegetPoolSize()){for(inti=getPoolSize();isize&&imaxPoolSize;i++){TaskManagethread=newTaskManage(this);thread.start();threads.add(thread);}}elseif(sizegetPoolSize()){while(getPoolSize()size){TaskManageth=(TaskManage)threads.remove(0);th.kill();}}}publicintgetPoolSize(){returnthreads.size();}protectedvoidnotifyForIdleThread(){hasIdleThread=true;}protectedbooleanwaitForIdleThread(){hasIdleThread=false;while(!hasIdleThread&&getPoolSize()=maxPoolSize){try{Thread.sleep(5);}catch(InterruptedExceptione){TaskException.getResultMessage(e);returnfalse;}}returntrue;}publicsynchronizedTaskManagegetIdleThread(){while(true){for(IteratorTaskManageitr=threads.iterator();itr.hasNext();){TaskManageth=(TaskManage)itr.next();if(!th.isRunning())returnth;}if(getPoolSize()maxPoolSize){TaskManagethread=newTaskManage(this);thread.start();threads.add(thread);returnthread;}if(waitForIdleThread()==false)returnnull;}}publicvoidprocessTask(Runnabletask){TaskManageth=getIdleThread();if(th!=null){th.putTask(task);th.startTasks();}}publicvoidprocessTasksInSingleThread(Runnable[]tasks){TaskManageth=getIdleThread();if(th!=null){th.putTasks(tasks);th.startTasks();}}publicvoidprocessTasksInSingleThread(CollectionRunnabletasks){TaskManageth=getIdleThread();if(th!=null){th.putTasks(tasks);th.startTasks();}}}复制代码代码如下:packagecom.smart.frame.task.autoTask;publicclassTopTaskimplementsRunnable{privateThreadPoolpool;publicTopTask(){super();}publicTopTask(ThreadPoolpool){super();this.pool=pool;}@Overridepublicvoidrun(){init();start();}/***初始化验证权限、参数之类*/publicvoidinit(){}/***开始自动任务*/publicvoidstart(){for(inti=0;i10;i++){pool.processTask(newBeginAuto());}}}/***实现类*/classBeginAutoimplementsRunnable{@Overridepublicvoidrun(){System.out.println(Thread.currentThread().getId()+..................);}}
本文标题:java中通用的线程池实例代码
链接地址:https://www.777doc.com/doc-2880657 .html