一个类android中可以使用多少个异步任务? [英] How many async task can be used in a class android?

查看:104
本文介绍了一个类android中可以使用多少个异步任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我参加了一次面试,被问到一个问题:一个班级可以使用多少个asynctask?通过使用execute方法,将通过调用asynctask使用.因此,一个类中asynctask的最大限制是向我提出的问题.

Recently I attended an interview in which I was asked a question: How many asynctask can be used in a class? By using execute method you will use by calling asynctask. So maximum limit of asynctask in a class is the question thrown to me.

答案是什么?有人可以解释一下为什么吗?

What is the answer for this? Can someone please explain how many and why?

推荐答案

问题本身没有任何意义.如果对类有限制,可以在类中使用任意多个AsyncTask.我认为他的意思是可以同时执行多少AsyncTask以及如何执行它们,答案是:这取决于.

The question itself does not make any sense. You can use as many AsyncTask in a class as you want, if there were a restriction on that it would be ridiculous. I assume he meant how many AsyncTask can be executed at the same time and how they are executed and the answer to that would be: It depends.

AsyncTasks可以串行或并行执行.默认行为取决于设备的API级别. 文档 AsyncTask的execute()的a>表示:

AsyncTasks can be executed either in series or in parallel. The default behaviour depends on the API level of the device. The documentation of execute() of AsyncTask says:

注意:此函数将任务安排在单个队列中 后台线程或线程池,具体取决于平台 版本.首次引入时,AsyncTasks在 单后台线程.从DONUT开始,已更改为 线程池,允许多个任务并行运行. 启动HONEYCOMB,任务又回到了一次执行的状态 线程,以避免并行引起的常见应用程序错误 执行.如果您确实想要并行执行,则可以使用 此方法的executeOnExecutor(Executor,Params ...)版本 THREAD_POOL_EXECUTOR;但是,请参阅那里的评论以获取警告 它的用途.

Note: this function schedules the task on a queue for a single background thread or pool of threads depending on the platform version. When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting HONEYCOMB, tasks are back to being executed on a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can use the executeOnExecutor(Executor, Params...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use.

话虽如此,您可以选择是要并行执行还是依次执行它们:

Having said that you can choose whether you want to execute them in parallel or in series like this:

// Executes the task in parallel to other tasks
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

// Adds the task to a queue and executes one at a time.
asyncTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);

但是,即使您并行运行任务,也可以同时运行多少个任务.要找出该限制在哪里,您必须查看

However even if you run the tasks in parallel there is a limit to how many can run at the same time. To find out where that limit is you have to look into the source code of AsyncTask.

直到Android 4.3(Jelly Bean),限制都已硬编码为这些值:

Up until Android 4.3 (Jelly Bean) the limits were hardcoded to those values:

private static final int CORE_POOL_SIZE = 5;
private static final int MAXIMUM_POOL_SIZE = 128;
private static final int KEEP_ALIVE = 1;

但是使用Android 4.4进行了更改,并且根据设备中使用的处理器来计算限制:

But with Android 4.4 that was changed and the limits are calculated depending on the used processor in the device:

private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
private static final int KEEP_ALIVE = 1;

ThreadPoolExecutor的实现在两种情况下均相同:

The implementation of the ThreadPoolExecutor remained the same in both cases:

public static final Executor THREAD_POOL_EXECUTOR
        = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,
                TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory);

因此,这应该可以回答您的问题.但是,如果您真的想了解AsyncTask的工作原理,那么您应该自己学习源代码! 此链接可在Android 4.4上实现AsyncTask实现 .

So that should pretty much answer your question. But if you really want to find out how the AsyncTask works then you should study the source code yourself! This link leads to the AsyncTask implementation on Android 4.4.

这篇关于一个类android中可以使用多少个异步任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆