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: 执行第三个