importjava.util.concurrent.Executors;importjava.util.concurrent.ExecutorService;publicclassThreadPoolDemo1 {publicstaticvoidmain(String[] args) {// 创建一个可重用固定线程数的线程池ExecutorService pool =Executors.newFixedThreadPool(2);// 创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口Thread ta =newMyThread();Thread tb =newMyThread();Thread tc =newMyThread();Thread td =newMyThread();Thread te =newMyThread();// 将线程放入池中进行执行pool.execute(ta);pool.execute(tb);pool.execute(tc);pool.execute(td);pool.execute(te);// 关闭线程池pool.shutdown(); }}classMyThreadextendsThread { @Overridepublicvoidrun() {System.out.println(Thread.currentThread().getName()+" is running."); }}运行结果:pool-1-thread-1 is running.pool-1-thread-2 is running.pool-1-thread-1 is running.pool-1-thread-2 is running.pool-1-thread-1 is running.结果说明:主线程中创建了线程池pool,线程池的容量是2。即,线程池中最多能同时运行2个线程。紧接着,将ta,tb,tc,td,te这3个线程添加到线程池中运行。最后,通过shutdown()关闭线程池。
importjava.util.concurrent.Executors;importjava.util.concurrent.ExecutorService;publicclassThreadPoolDemo1 {publicstaticvoidmain(String[] args) {// 创建一个可重用固定线程数的线程池ExecutorService pool =Executors.newFixedThreadPool(2);// 创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口Thread ta =newMyThread();Thread tb =newMyThread();Thread tc =newMyThread();Thread td =newMyThread();Thread te =newMyThread();// 将线程放入池中进行执行pool.execute(ta);pool.execute(tb);pool.execute(tc);pool.execute(td);pool.execute(te);// 关闭线程池pool.shutdown(); }}classMyThreadextendsThread { @Overridepublicvoidrun() {System.out.println(Thread.currentThread().getName()+" is running."); }}运行结果:pool-1-thread-1 is running.pool-1-thread-2 is running.pool-1-thread-1 is running.pool-1-thread-2 is running.pool-1-thread-1 is running.示例中,包括了线程池的创建,将任务添加到线程池中,关闭线程池这3个主要的步骤。
importjava.lang.reflect.Field;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.ThreadPoolExecutor.DiscardPolicy;publicclassDiscardPolicyDemo {privatestaticfinalint THREADS_SIZE =1;privatestaticfinalint CAPACITY =1;publicstaticvoidmain(String[] args) throwsException {// 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(THREADS_SIZE),"线程池"的阻塞队列容量为1(CAPACITY)。ThreadPoolExecutor pool =newThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE,0,TimeUnit.SECONDS,newArrayBlockingQueue<Runnable>(CAPACITY));// 设置线程池的拒绝策略为"丢弃"pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());// 新建10个任务,并将它们添加到线程池中。for (int i =0; i <10; i++) {Runnable myrun =newMyRunnable("task-"+i);pool.execute(myrun); }// 关闭线程池pool.shutdown(); }}classMyRunnableimplementsRunnable {privateString name;publicMyRunnable(String name) {this.name= name; } @Overridepublicvoidrun() {try {System.out.println(this.name+" is running.");Thread.sleep(100); } catch (Exception e) {e.printStackTrace(); } }}运行结果:task-0 is running.task-1 is running.结果说明:线程池pool的"最大池大小"和"核心池大小"都为1(THREADS_SIZE),这意味着"线程池能同时运行的任务数量最大只能是1"。线程池pool的阻塞队列是ArrayBlockingQueue,ArrayBlockingQueue是一个有界的阻塞队列,ArrayBlockingQueue的容量为1。这也意味着线程池的阻塞队列只能有一个线程池阻塞等待。根据""中分析的execute()代码可知:线程池中共运行了2个任务。第1个任务直接放到Worker中,通过线程去执行;第2个任务放到阻塞队列中等待。其他的任务都被丢弃了!
DiscardOldestPolicy 示例
importjava.lang.reflect.Field;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy;publicclassDiscardOldestPolicyDemo {privatestaticfinalint THREADS_SIZE =1;privatestaticfinalint CAPACITY =1;publicstaticvoidmain(String[] args) throwsException {// 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(THREADS_SIZE),"线程池"的阻塞队列容量为1(CAPACITY)。ThreadPoolExecutor pool =newThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE,0,TimeUnit.SECONDS,newArrayBlockingQueue<Runnable>(CAPACITY));// 设置线程池的拒绝策略为"DiscardOldestPolicy"pool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());// 新建10个任务,并将它们添加到线程池中。for (int i =0; i <10; i++) {Runnable myrun =newMyRunnable("task-"+i);pool.execute(myrun); }// 关闭线程池pool.shutdown(); }}classMyRunnableimplementsRunnable {privateString name;publicMyRunnable(String name) {this.name= name; } @Overridepublicvoidrun() {try {System.out.println(this.name+" is running.");Thread.sleep(200); } catch (Exception e) {e.printStackTrace(); } }}运行结果:task-0 is running.task-9 is running.结果说明:将"线程池的拒绝策略"由DiscardPolicy修改为DiscardOldestPolicy之后,当有任务添加到线程池被拒绝时,线程池会丢弃阻塞队列中末尾的任务,然后将被拒绝的任务添加到末尾。
AbortPolicy 示例
importjava.lang.reflect.Field;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.ThreadPoolExecutor.AbortPolicy;importjava.util.concurrent.RejectedExecutionException;publicclassAbortPolicyDemo {privatestaticfinalint THREADS_SIZE =1;privatestaticfinalint CAPACITY =1;publicstaticvoidmain(String[] args) throwsException {// 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(THREADS_SIZE),"线程池"的阻塞队列容量为1(CAPACITY)。ThreadPoolExecutor pool =newThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE,0,TimeUnit.SECONDS,newArrayBlockingQueue<Runnable>(CAPACITY));// 设置线程池的拒绝策略为"抛出异常"pool.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());try {// 新建10个任务,并将它们添加到线程池中。for (int i =0; i <10; i++) {Runnable myrun =newMyRunnable("task-"+i);pool.execute(myrun); } } catch (RejectedExecutionException e) {e.printStackTrace();// 关闭线程池pool.shutdown(); } }}classMyRunnableimplementsRunnable {privateString name;publicMyRunnable(String name) {this.name= name; } @Overridepublicvoidrun() {try {System.out.println(this.name+" is running.");Thread.sleep(200); } catch (Exception e) {e.printStackTrace(); } }}(某一次)运行结果:java.util.concurrent.RejectedExecutionException at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1774) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:768) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:656) at AbortPolicyDemo.main(AbortPolicyDemo.java:27)task-0 is running.task-1 is running.结果说明:将"线程池的拒绝策略"由DiscardPolicy修改为AbortPolicy之后,当有任务添加到线程池被拒绝时,会抛出RejectedExecutionException。
CallerRunsPolicy 示例
importjava.lang.reflect.Field;importjava.util.concurrent.ArrayBlockingQueue;importjava.util.concurrent.ThreadPoolExecutor;importjava.util.concurrent.TimeUnit;importjava.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;publicclassCallerRunsPolicyDemo {privatestaticfinalint THREADS_SIZE =1;privatestaticfinalint CAPACITY =1;publicstaticvoidmain(String[] args) throwsException {// 创建线程池。线程池的"最大池大小"和"核心池大小"都为1(THREADS_SIZE),"线程池"的阻塞队列容量为1(CAPACITY)。ThreadPoolExecutor pool =newThreadPoolExecutor(THREADS_SIZE, THREADS_SIZE,0,TimeUnit.SECONDS,newArrayBlockingQueue<Runnable>(CAPACITY));// 设置线程池的拒绝策略为"CallerRunsPolicy"pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());// 新建10个任务,并将它们添加到线程池中。for (int i =0; i <10; i++) {Runnable myrun =newMyRunnable("task-"+i);pool.execute(myrun); }// 关闭线程池pool.shutdown(); }}classMyRunnableimplementsRunnable {privateString name;publicMyRunnable(String name) {this.name= name; } @Overridepublicvoidrun() {try {System.out.println(this.name+" is running.");Thread.sleep(100); } catch (Exception e) {e.printStackTrace(); } }}(某一次)运行结果:task-2 is running.task-3 is running.task-4 is running.task-5 is running.task-6 is running.task-7 is running.task-8 is running.task-9 is running.task-0 is running.task-1 is running.结果说明:将"线程池的拒绝策略"由DiscardPolicy修改为CallerRunsPolicy之后,当有任务添加到线程池被拒绝时,线程池会将被拒绝的任务添加到"线程池正在运行的线程"中取运行。