如何分别运行两个独立的异步任务?

时间:2021-10-17 14:04:20

In my application I have two button clicks and in both button clicks there are two different Asynchronous tasks are running ,and my problem is when i click button 1 and first asynchronous task is running mean while its running i clicked second button and second asynchronous task also started but in background till the first asynchronous task is completed the second asynchronous task is not starting ?

在我的应用程序中,我有两个按钮单击,在两个按钮单击中有两个不同的异步任务正在运行,我的问题是当我单击按钮1并且第一个异步任务正在运行意味着它运行时我点击了第二个按钮和第二个异步任务也开始但在后台直到第一个异步任务完成第二个异步任务没有启动?

public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
if(position==1)
           {

              Asynchronoustask1();

           }

            if(position==2)
            {

              Asynchronoustask2();
            }
}

3 个解决方案

#1


5  

If your implementation for AsyncTask is correct, then the possible reason for this behaviour is because the default executor for AsyncTasks is SERIAL_EXECUTOR.

如果AsyncTask的实现是正确的,那么这种行为的可能原因是因为AsyncTasks的默认执行程序是SERIAL_EXECUTOR。

To run multiple AsyncTask instances in parallel (not serially.. no one after other). execute them as follows:

并行运行多个AsyncTask实例(不是串行运行的......没有一个接一个)。按如下方式执行:

if (Build.VERSION.SDK_INT >= 11) {
    outstandingTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    outstandingTask.execute();
}

From the documentation:

从文档:

When first introduced, AsyncTask 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 with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

首次引入时,AsyncTask在单个后台线程上串行执行。从DONUT开始,这被改为一个线程池,允许多个任务并行运行。从HONEYCOMB开始,任务在单个线程上执行,以避免由并行执行引起的常见应用程序错误。

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent. Executor, Object[]) with THREAD_POOL_EXECUTOR.

如果您真的想要并行执行,可以使用THREAD_POOL_EXECUTOR调用executeOnExecutor(java.util.concurrent .executor,Object [])。

#2


0  

There are multiple Options to run asyncrhonously Either you can use two AsyncTask which contains a method called doInBackground() for your use. Or simple you can use two threads.

有多个选项可以asyncrhonously运行您可以使用两个AsyncTask包含一个名为doInBackground()的方法供您使用。或者简单,你可以使用两个线程。

Eg: Thread

例如:线程

Thread thread = new Thread()
{
@Override
public void run() {
    try {
        while(true) {
           // your code
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
};

thread.start();

If you want any delay Use Handler.

如果您想要任何延迟使用处理程序。

Or

要么

You need to do any UI Operations you should use RunOnUIThread()

你需要做任何你应该使用的UI操作RunOnUIThread()

#3


0  

Asynchronous task always run in background.It will not disturb the any process.Might be your first Asynchronous task holding some resources and that one also using for second one.So its waiting for the resource.

异步任务总是在后台运行。它不会打扰任何进程。可能是你的第一个异步任务持有一些资源,而另一个也用于第二个。所以它等待资源。

Please have look @ your code carefully or paste your code..

请仔细查看@您的代码或粘贴您的代码..

#1


5  

If your implementation for AsyncTask is correct, then the possible reason for this behaviour is because the default executor for AsyncTasks is SERIAL_EXECUTOR.

如果AsyncTask的实现是正确的,那么这种行为的可能原因是因为AsyncTasks的默认执行程序是SERIAL_EXECUTOR。

To run multiple AsyncTask instances in parallel (not serially.. no one after other). execute them as follows:

并行运行多个AsyncTask实例(不是串行运行的......没有一个接一个)。按如下方式执行:

if (Build.VERSION.SDK_INT >= 11) {
    outstandingTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    outstandingTask.execute();
}

From the documentation:

从文档:

When first introduced, AsyncTask 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 with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

首次引入时,AsyncTask在单个后台线程上串行执行。从DONUT开始,这被改为一个线程池,允许多个任务并行运行。从HONEYCOMB开始,任务在单个线程上执行,以避免由并行执行引起的常见应用程序错误。

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent. Executor, Object[]) with THREAD_POOL_EXECUTOR.

如果您真的想要并行执行,可以使用THREAD_POOL_EXECUTOR调用executeOnExecutor(java.util.concurrent .executor,Object [])。

#2


0  

There are multiple Options to run asyncrhonously Either you can use two AsyncTask which contains a method called doInBackground() for your use. Or simple you can use two threads.

有多个选项可以asyncrhonously运行您可以使用两个AsyncTask包含一个名为doInBackground()的方法供您使用。或者简单,你可以使用两个线程。

Eg: Thread

例如:线程

Thread thread = new Thread()
{
@Override
public void run() {
    try {
        while(true) {
           // your code
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
};

thread.start();

If you want any delay Use Handler.

如果您想要任何延迟使用处理程序。

Or

要么

You need to do any UI Operations you should use RunOnUIThread()

你需要做任何你应该使用的UI操作RunOnUIThread()

#3


0  

Asynchronous task always run in background.It will not disturb the any process.Might be your first Asynchronous task holding some resources and that one also using for second one.So its waiting for the resource.

异步任务总是在后台运行。它不会打扰任何进程。可能是你的第一个异步任务持有一些资源,而另一个也用于第二个。所以它等待资源。

Please have look @ your code carefully or paste your code..

请仔细查看@您的代码或粘贴您的代码..