简体   繁体   English

在一个AsyncTask实例上多次调用execute()时,线程是否同步运行?

[英]Do threads run synchronously when calling execute() multiple times on one AsyncTask instance

So I have this bgTask class that extends AsyncTask, and I call the constructor once. 因此,我有一个扩展AsyncTask的bgTask类,并且一次调用了构造函数。 But then I call bgTask.execute() inside a for loop. 但是然后我在for循环中调用bgTask.execute()。 Do the threads run synchronously in this case? 在这种情况下,线程是否同步运行? And I do use the onPostExecute method inside the class. 而且我确实在类内部使用了onPostExecute方法。

BgTask bgTask = new BgTask();

for (int i = 0; i < 10; i++) {
   bgTask.execute();
}

when you call execute it runs the AsyncTask immediately. 当您调用execute时,它将立即运行AsyncTask。 If you need a group of threads to behave a certain way then I would suggest reviewing Java's Executors specifically Thread Pools . 如果您需要一组线程以某种方式运行,那么我建议您回顾一下Java的Executors特别是Thread Pools

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM