标签:des android style blog color 使用 java io ar
public class Executors { public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory); } }虽然我一直认同程序员应使用较为方便的Executors工厂方法Executors.newCachedThreadPool() (无界线程池,可以进行自动线程回收)、Executors.newFixedThreadPool(int)(固定大小线程池)和Executors.newSingleThreadExecutor()(单个后台线程),但是通过源码我们可以发现最后他们均调用了ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) 方法,因此我们在分析java.util.concurrent.RejectedExecutionException之前,需要深入学习一下ThreadPoolExecutor的使用。
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TextExecutor { public ExecutorService fixedExecutorService = Executors.newFixedThreadPool(5); public ExecutorService cachedExecutorService = Executors.newCachedThreadPool(); public ExecutorService singleExecutorService = Executors.newSingleThreadExecutor(); public void testExecutorException() { for (int i = 0; i < 10; i ++) { fixedExecutorService.execute(new SayHelloRunnable()); fixedExecutorService.shutdown(); } } private class SayHelloRunnable implements Runnable { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { System.out.println("hello world!"); } } } public static void main(String[] args) { TextExecutor testExecutor = new TextExecutor(); testExecutor.testExecutorException(); } }
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TextExecutor { public ExecutorService fixedExecutorService = Executors.newFixedThreadPool(5); public ExecutorService cachedExecutorService = Executors.newCachedThreadPool(); public ExecutorService singleExecutorService = Executors.newSingleThreadExecutor(); public void testExecutorException() { for (int i = 0; i < 10; i ++) { // 增加isShutdown()判断 if (!fixedExecutorService.isShutdown()) { fixedExecutorService.execute(new SayHelloRunnable()); } fixedExecutorService.shutdown(); } } private class SayHelloRunnable implements Runnable { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { System.out.println("hello world!"); } } } public static void main(String[] args) { TextExecutor testExecutor = new TextExecutor(); testExecutor.testExecutorException(); } }
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class TextExecutor { public ExecutorService fixedExecutorService = Executors.newFixedThreadPool(5); public ExecutorService cachedExecutorService = Executors.newCachedThreadPool(); public ExecutorService singleExecutorService = Executors.newSingleThreadExecutor(); public ExecutorService customerExecutorService = new ThreadPoolExecutor(3, 5, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); public void testExecutorException() { for (int i = 0; i < 10; i ++) { // 增加isShutdown()判断 if (!fixedExecutorService.isShutdown()) { fixedExecutorService.execute(new SayHelloRunnable()); } fixedExecutorService.shutdown(); } } public void testCustomerExecutorException() { for (int i = 0; i < 100; i ++) { customerExecutorService.execute(new SayHelloRunnable()); } } private class SayHelloRunnable implements Runnable { @Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { System.out.println("hello world!"); } } } public static void main(String[] args) { TextExecutor testExecutor = new TextExecutor(); testExecutor.testCustomerExecutorException();; } }
public ExecutorService customerExecutorService = new ThreadPoolExecutor(3, Integer.MAX_VALUE, 0, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());2. 使用其他排队策略,例如LinkedBlockingQueue
<span style="white-space:pre"> </span>public ExecutorService customerExecutorService = new ThreadPoolExecutor(3, 5, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
解决java.util.concurrent.RejectedExecutionException
标签:des android style blog color 使用 java io ar
原文地址:http://blog.csdn.net/wzy_1988/article/details/38922449