Android 对线程池的使用
来源:互联网 发布:工业大数据 李杰 下载 编辑:程序博客网 时间:2024/06/05 16:39
了解这几个接口Executor,
接口ExecutorService继承 Executor,
ThreadPoolExecutor,Executors;
Executors 工厂类 用于创建各种类型的线程池。其包含的方法
创建固定个数线程的线程池
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); }
创建一个线程的线程池
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are
available.
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); }
使用过程
内核数量,一般创建线程的个数为内核个数+1 最大线程数为内核数量*2 +1
int processors = Runtime.getRuntime().availableProcessors(); System.out.println("cpu 数量:"+processors);
//创建一个线程池,包含最多两个线程的固定线程池 ExecutorService executorService = Executors.newFixedThreadPool(2); ThreadPoolExecutor ThreadPool = new ThreadPoolExecutor(2, 2, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); ThreadPool.execute(new Runnable() { @Override public void run() { while (true) { System.out.println("执行ThreadPool线程池"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); //添加了第一个任务...这个任务会一直被执行... executorService.execute(new Runnable() { @Override public void run() { while (true) { System.out.println("执行第一个"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); //添加第二个任务,被执行三次停止... executorService.execute(new Runnable() { @Override public void run() { int i = 0; while (true) { i++; System.out.println("执行第二个"); if (i == 3) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }); executorService.execute(new Runnable() { @Override public void run() { while (true) { System.out.println("执行第三个"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
结果如下1:
10-30 15:44:43.287 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:43.288 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:43.289 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第二个10-30 15:44:44.288 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:44.289 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:44.290 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第二个
如下2:
10-30 15:44:45.288 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:45.290 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:45.290 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第二个10-30 15:44:45.290 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第三个10-30 15:44:46.289 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:46.290 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:46.290 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第三个10-30 15:44:47.289 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:47.290 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:47.291 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第三个10-30 15:44:48.289 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:48.291 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:48.291 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第三个10-30 15:44:49.290 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:49.291 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:49.292 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第三个10-30 15:44:50.290 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:50.292 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第三个10-30 15:44:50.292 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:51.291 3967-4102/com.wall.geat.greatapplication I/System.out: 执行ThreadPool线程池10-30 15:44:51.292 3967-4103/com.wall.geat.greatapplication I/System.out: 执行第一个10-30 15:44:51.292 3967-4104/com.wall.geat.greatapplication I/System.out: 执行第三个
阅读全文
0 0
- Android 对线程池的使用
- android中对线程池的理解与使用
- android中对线程池的理解与使用
- android中对线程池的理解与使用
- android 对线程池的探究
- android和java中对线程的使用总结
- Android线程池的使用
- Android线程池的使用
- Android 线程池的使用
- Android线程池的使用
- Android 线程池的使用
- Android 线程池的使用
- Android线程的使用
- android中关于对线程池的理解以及操作
- Android开发之对线程池的理解
- ICE使用笔记---对线程池模式的浅析
- 创建线程的另一种方法-使用Android线程池
- Android开发中线程和线程池的使用
- java基础之集合框架--Collection及Map顶级接口
- linux查看某个时间段的日志
- react-native 屏幕尺寸和文字大小适配
- Java垃圾回收机制
- A wizard’s guide to Adversarial Autoencoders: Part 1, Autoencoder?
- Android 对线程池的使用
- 西雅图式的爱情
- js获取点击事件的位置,兼容主流浏览器
- Redis 键(Key)
- hibernate annotation注解方式来处理映射关系
- 摆花 洛谷p1077
- 用OpenCV的函数bilateralFilter做双边滤波~
- Util工具类 获取某日期区间的所有日期
- iOS算法总结-冒泡排序