乡村旅游网站建设,客户管理系统的需求分析,做同城网站,图片类网站开发实验总结引入线程池的好处 1#xff09;提升性能。创建和消耗对象费时费CPU资源 2#xff09;防止内存过度消耗。控制活动线程的数量#xff0c;防止并发线程过多。 我们来看一下线程池的简单的构造 [html] view plaincopy print?public ThreadPoolExecutor(int corePoolSize, … 引入线程池的好处 1提升性能。创建和消耗对象费时费CPU资源 2防止内存过度消耗。控制活动线程的数量防止并发线程过多。 我们来看一下线程池的简单的构造 [html] view plaincopy print? public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {...} 使用上面的方式创建线程池的话我们需要配置一堆东西非常麻烦所以我们不建议这么使用。而是推荐使用Executors的工厂方法来创建线程池Executors类是官方提供的一个工厂类它里面封装好了众多功能不一样的线程池。下面就介绍几个常用的线程池。 [html] view plaincopy print? public ThreadPoolExecutor( //核心线程数除非allowCoreThreadTimeOut被设置为true否则它闲着也不会死 int corePoolSize, //最大线程数活动线程数量超过它后续任务就会排队 int maximumPoolSize, //超时时长作用于非核心线程allowCoreThreadTimeOut被设置为true时也会同时作用于核心线程闲置超时便被回收 long keepAliveTime, //枚举类型设置keepAliveTime的单位有TimeUnit.MILLISECONDSms、TimeUnit. SECONDSs等 TimeUnit unit, //缓冲任务队列线程池的execute方法会将Runnable对象存储起来 BlockingQueueRunnable workQueue, //线程工厂接口只有一个new Thread(Runnable r)方法可为线程池创建新线程 ThreadFactory threadFactory) 1、FixedThreadPool() 该方法返回一个固定线程数量的线程池该线程池中的线程数量始终不变即不会再创建新的线程也不会销毁已经创建好的线程自始自终都是那几个固定的线程在工作所以该线程池可以控制线程的最大并发数。 栗子假如有一个新任务提交时线程池中如果有空闲的线程则立即使用空闲线程来处理任务如果没有则会把这个新任务存在一个任务队列中一旦有线程空闲了则按FIFO方式处理任务队列中的任务。 [html] view plaincopy print? public static ExecutorService newFixThreadPool(int nThreads){ return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueueRunnable()); } //使用 Executors.newFixThreadPool(5).execute(r); 2、CachedThreadPool() 该方法返回一个可以根据实际情况调整线程池中线程的数量的线程池。即该线程池中的线程数量不确定是根据实际情况动态调整的。 栗子假如该线程池中的所有线程都正在工作而此时有新任务提交那么将会创建新的线程去处理该任务而此时假如之前有一些线程完成了任务现在又有新任务提交那么将不会创建新线程去处理而是复用空闲的线程去处理新任务。那么此时有人有疑问了那这样来说该线程池的线程岂不是会越集越多其实并不会因为线程池中的线程都有一个“保持活动时间”的参数通过配置它如果线程池中的空闲线程的空闲时间超过该“保存活动时间”则立刻停止该线程而该线程池默认的“保持活动时间”为60s。 [html] view plaincopy print? public static ExecutorService newCachedThreadPool(int nThreads){ return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit. SECONDS, new SynchronousQueueRunnable()); } //使用 Executors.newCachedThreadPool().execute(r); 3、SingleThreadExecutor() 该方法返回一个只有一个线程的线程池即每次只能执行一个线程任务多余的任务会保存到一个任务队列中等待这一个线程空闲当这个线程空闲了再按FIFO方式顺序执行任务队列中的任务。 [html] view plaincopy print? public static ExecutorService newSingleThreadPool (int nThreads){ return new FinalizableDelegatedExecutorService ( new ThreadPoolExecutor (1, 1, 0, TimeUnit. MILLISECONDS, new LinkedBlockingQueueRunnable()) ); } //使用 Executors.newSingleThreadPool ().execute(r); 4、ScheduledThreadPool() 该方法返回一个可以控制线程池内线程定时或周期性执行某任务的线程池。 [html] view plaincopy print? public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize){ return new ScheduledThreadPoolExecutor(corePoolSize); } public ScheduledThreadPoolExecutor(int corePoolSize){ super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedQueue ()); } //使用延迟1秒执行每隔2秒执行一次Runnable r Executors. newScheduledThreadPool (5).scheduleAtFixedRate(r, 1000, 2000, TimeUnit.MILLISECONDS); 自定义线程池 Android中常用的线程池就上面的四种其实在Java中还有一种常见的线程池newSingleThreadScheduledExecutor其实上面的线程池对于我们开发已经是足够了不过有时候上面的仍然不能满足我们这时候我们就需要自定义不同功能的线程池。上面我们也说了线程池功能的不同归根到底还是内部的BlockingQueue实现不同所以我们要实现我们自己相要的线程池就必须从BlockingQueue的实现上做手脚。 那么我们接下来就用PriorityBlockingQueue来实现一个FIFO的线程池。 1创建一个基于PriorityBlockingQueue的线程池 [html] view plaincopy print? ExecutorService priorityThreadPool new ThreadPoolExecutor(3,3,0L,TimeUnit.SECONDS,new PriorityBlockingQueue()); 2创建一个实现Runnable接口的类并向外提供我们实现自定义功能并实现Comparable接口 [html] view plaincopy print? public abstract class PriorityRunnable implements Runnable, Comparable { private int priority; public PriorityRunnable(int priority) { if (priority 0) throw new IllegalArgumentException(); this.priority priority; } Override public int compareTo(PriorityRunnable another) { int my this.getPriority(); int other another.getPriority(); return my 1 : my other ? -1 : 0; } Override public void run() { doSth(); } public abstract void doSth(); public int getPriority() { return priority; } } 3使用PriorityRunnable提交任务 [html] view plaincopy print? ExecutorService priorityThreadPool new ThreadPoolExecutor(3, 3, 0L, TimeUnit.SECONDS, new PriorityBlockingQueue()); for (int i 1; i 10; i) { final int priority i; priorityThreadPool.execute(new PriorityRunnable(priority) { Override public void doSth() { String threadName Thread.currentThread().getName(); Log.v(zxy, 线程 threadName ,正在执行优先级为 priority 的任务); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } 欢迎大家加移动技术群278792776AndroidIOS,RN后台全搞定