当前位置:   article > 正文

Android 线程池ThreadPool的基本使用_android 线程池使用

android 线程池使用

此文章是参考其它文章写的,因为本人之前没怎么接触过线程池,所以可能与其它文章相似。在这里只是做一个记录,以后继续深入学习!

我在写异步时,经常这样写:

  1. new Thread(new Runnable() {
  2. @Override
  3. public void run() {
  4. // TODO
  5. }
  6. }).start();

这样new出来的匿名对象会存在一些问题:

  1. 由于是匿名的,无法对它进行管理
  2. 如果需要多次执行这个操作就new多次,可能创建多个,占用系统资源
  3. 无法执行更多的操作

所以,使用线程池的好处:

  1. 可以重复利用存在的线程,减少系统的开销
  2. 利用线程池可以执行定时、并发数的控制

原理:根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果;少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。若队列中没有等待进程,线程池的这一资源处于等待。当一个新任务需要运行时,如果线程池中有等待的工作线程,就可以开始运行了;否则进入等待队列。

四种线程池

(1)newCachedThreadPool:

创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。

(2)newFixedThreadPool:

创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。

(3)newScheduledThreadPool:

创建一个定长线程池,支持定时及周期性任务执行。

(4)newSingleThreadExecutor:

创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

1.newCachedThreadPool

  1. /*
  2. * 可以缓存线程池
  3. */
  4. public static void Function1() {
  5. ExecutorService executorService = Executors.newCachedThreadPool();
  6. for (int i = 0; i < 30; i++) {//30次
  7. final int index = i;
  8. try {
  9. Thread.sleep(100); // 休眠时间越短创建的线程数越多
  10. } catch (InterruptedException e) {
  11. // TODO Auto-generated catch block
  12. e.printStackTrace();
  13. }
  14. executorService.execute(new Runnable() {
  15. @Override
  16. public void run() {
  17. // TODO Auto-generated method stub
  18. System.out.println("active count = " + Thread.activeCount()
  19. + " index = " + index);
  20. try {
  21. Thread.sleep(1000);
  22. } catch (InterruptedException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }
  26. }
  27. });
  28. }
  29. }

打印结果:

  1. active count = 2 index = 0
  2. active count = 3 index = 1
  3. active count = 4 index = 2
  4. active count = 5 index = 3
  5. active count = 6 index = 4
  6. active count = 7 index = 5
  7. active count = 8 index = 6
  8. active count = 9 index = 7
  9. active count = 10 index = 8
  10. active count = 11 index = 9
  11. active count = 11 index = 10
  12. active count = 11 index = 11
  13. active count = 11 index = 12
  14. active count = 11 index = 13
  15. active count = 11 index = 14
  16. active count = 11 index = 15
  17. active count = 11 index = 16
  18. active count = 11 index = 17
  19. active count = 11 index = 18
  20. active count = 11 index = 19
  21. active count = 11 index = 20
  22. active count = 11 index = 21
  23. active count = 11 index = 22
  24. active count = 11 index = 23
  25. active count = 11 index = 24
  26. active count = 11 index = 25
  27. active count = 11 index = 26
  28. active count = 11 index = 27
  29. active count = 11 index = 28
  30. active count = 11 index = 29
  31. active count = 10 index = 30

从打印消息来看开始线程数在增加,后来稳定,可以修改休眠时间,休眠时间越短创建的线程数就越多,因为前面的还没执行完,线程池中没有可以执行的就需要创建;如果把休眠时间加大,创建的线程数就会少

2.newFixedThreadPool

根据传入的参数创建线程数目

  1. /**
  2. * 定长线程池
  3. */
  4. public static void Function2 () {
  5. ExecutorService executorService = Executors.newFixedThreadPool(3);
  6. for (int i = 0; i < 30; i++) {
  7. final int index = i;
  8. executorService.execute(new Runnable() {
  9. @Override
  10. public void run() {
  11. try {
  12. System.out.println("index = " + index
  13. + " thread count = " + Thread.activeCount());
  14. Thread.sleep(2000);
  15. } catch (InterruptedException e) {
  16. // TODO Auto-generated catch block
  17. e.printStackTrace();
  18. }
  19. }
  20. });
  21. }
  22. }

3.newScheduledThreadPool

  1. /**
  2. * 定长线程池,可做延时
  3. */
  4. public static void Function3 () {
  5. ScheduledExecutorService executorService = Executors
  6. .newScheduledThreadPool(5);
  7. executorService.schedule(new Runnable() {
  8. @Override
  9. public void run() {
  10. System.out.println("delay 3 seconds" + " thread count = "
  11. + Thread.activeCount());
  12. }
  13. }, 3, TimeUnit.SECONDS);
  14. }
  15. /**
  16. * 定期执行,可以用来做定时器
  17. */
  18. public static void Function4 () {
  19. ScheduledExecutorService executorService = Executors
  20. .newScheduledThreadPool(3);
  21. executorService.scheduleAtFixedRate(new Runnable() {
  22. @Override
  23. public void run() {
  24. System.out
  25. .println("delay 1 seconds, and excute every 3 seconds"
  26. + " thread count = " + Thread.activeCount());
  27. }
  28. }, 1, 3, TimeUnit.SECONDS);
  29. }

打印结果:

  1. delay 1 seconds, and excute every 3 seconds thread count = 2
  2. delay 1 seconds, and excute every 3 seconds thread count = 3
  3. delay 1 seconds, and excute every 3 seconds thread count = 4
  4. delay 1 seconds, and excute every 3 seconds thread count = 4
  5. delay 1 seconds, and excute every 3 seconds thread count = 4
  6. delay 1 seconds, and excute every 3 seconds thread count = 4
  7. delay 1 seconds, and excute every 3 seconds thread count = 4
  8. delay 1 seconds, and excute every 3 seconds thread count = 4
  9. delay 1 seconds, and excute every 3 seconds thread count = 4

4.newSingleThreadExecutor这个最简单

  1. /**
  2. * 单例线程
  3. */
  4. public static void Function5() {
  5. ExecutorService singleThreadExecutor = Executors
  6. .newSingleThreadExecutor();
  7. for (int i = 0; i < 5; i++) {
  8. final int index = i;
  9. singleThreadExecutor.execute(new Runnable() {
  10. @Override
  11. public void run() {
  12. try {
  13. System.out.println("index = " + index
  14. + " thread count = " + Thread.activeCount());
  15. Thread.sleep(1000);
  16. } catch (InterruptedException e) {
  17. // TODO Auto-generated catch block
  18. e.printStackTrace();
  19. }
  20. }
  21. });
  22. }
  23. }

打印结果:

  1. index = 0 thread count = 2
  2. index = 1 thread count = 2
  3. index = 2 thread count = 2
  4. index = 3 thread count = 2
  5. index = 4 thread count = 2

只创建了一个线程。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/240444
推荐阅读
相关标签
  

闽ICP备14008679号