zoukankan      html  css  js  c++  java
  • 关于Android的asynctask-threads-limits问题:asynctask开启的线程是否有极限

    http://stackoverflow.com/questions/9654148/android-asynctask-threads-limits

    antonyt:

    All AsyncTasks are controlled internally by a shared (static) ThreadPoolExecutor and aLinkedBlockingQueue. When you call execute on an AsyncTask, the ThreadPoolExecutor will execute it when it is ready some time in the future.

    The 'when am I ready?' behaviour of a ThreadPoolExecutor is controlled by two parameters, the core pool size and the maximum pool size. If there are less than core pool size threads currently active and a new job comes in, the executor will create a new thread and execute it immediately. If there are at least core pool size threads running, it will try to queue the job and wait until there is an idle thread available (i.e. until another job is completed). If it is not possible to queue the job (the queue can have a max capacity), it will create a new thread (upto maximum pool size threads) for the jobs to run in. Non-core idle threads can eventually be decommissioned according to a keep-alive timeout parameter.

    Before Android 1.6, the core pool size was 1 and the maximum pool size was 10. Since Android 1.6, the core pore size is 5, and the maximum pool size is 128. The size of the queue is 10 in both cases. The keep-alive timeout was 10 seconds before 2.3, and 1 second since then.

    With all of this in mind, it now becomes clear why the AsyncTask will only appear to execute 5/6 of your tasks. The 6th task is being queued up until one of the other tasks complete. This is a very good reason why you should not use AsyncTasks for long-running operations - it will prevent other AsyncTasks from ever running.

    For completeness, if you repeated your exercise with more than 6 tasks (e.g. 30), you will see that more than 6 will enter doInBackground as the queue will become full and the executor is pushed to create more worker threads. If you kept with the long-running task, you should see that 20/30 become active, with 10 still in the queue.

    Android会开启一个线程池专门管理子线程启动问题,当线程池内线程数小于最大值时会启动线程,当线程池满时,当前线程未结束其他线程则要等待。

  • 相关阅读:
    [线段树][数学]JZOJ 4237 Melancholy
    [规律]JZOJ 4222 恐怖的奴隶主
    [Tarjan][基环树]JZOJ 4221 互相追逐的点
    [斯特林数][自然数幂和]JZOJ 4220 WYF的盒子
    奇妙的骚操作
    [树形DP][概率期望]JZOJ 4225 宝藏
    操作系统基础知识
    计算机硬件知识整理
    ORM的查询
    ORM的记录添加和删除
  • 原文地址:https://www.cnblogs.com/mada0/p/4889680.html
Copyright © 2011-2022 走看看