理解android中的并行异步任务 [英] understanding parallel Async Task in android

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

问题描述

在我的应用程序中,我在很多地方都使用了异步任务,最近我遇到了下面异步任务的 doInBackground 没有被调用的问题,这是在我第三次执行下面的异步任务时发生的(其他异步任务也在运行).

In my application I am using Async tasks in many places, recently I am facing problem where doInBackground of the below Async task is not getting called, this is happening when I execute the below Async task for the 3rd time (other Async tasks are also running).

protected class StatusTask extends AsyncTask<Void, Void, Void> {
    private final BluetoothDevice device;

    public StatusTask(BluetoothDevice device) {
        this.device = device;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        while (true) {
            if (someCondition) {
                Log.D(TAG,"hi hello")
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        tTask=null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        System.out.println("onCancelled " + this.isCancelled());
    }

    @Override
    protected void onCancelled(Void aVoid) {
        super.onCancelled(aVoid);
        System.out.println("onCancelled result " + this.isCancelled());
    }
}

我正在执行上述任务,

 StatusTask  tTask = new StatusTask(device);   
 tTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

我在 Android 版本 4.4.3 上运行.

I am running on Android version 4.4.3.

  1. 我如何知道我的应用程序中当前正在运行多少同步任务?Bcz 我猜 CORE_POOL_SIZE 计数可能导致问题.

  1. How do I find out how many Sync tasks are running currently in my app? Bcz I guess CORE_POOL_SIZE count might be causing the problem.

或者如果发生死锁或任何其他情况并阻止执行新的异步任务,android studio 中是否有办法识别并终止其他可能没有用的异步任务?

Or if deadlock or any other situation is happened and blocking new Async task to get executed, is there a way in android studio to identity and kill the other Async tasks which may not be useful?

如果我使用 CustomThreadPoolExecutor 作为 disconnectTask.executeOnExecutor(mCustomThreadPoolExecutor);使用更多 CorePoolSize 其工作正常,但下一个使用 AsyncTask.THREAD_POOL_EXECUTOR 的异步任务的 doInBackground() 没有被调用.

If I use CustomThreadPoolExecutor as disconnectTask.executeOnExecutor(mCustomThreadPoolExecutor); with more CorePoolSize its working fine but doInBackground() of next Async task which is using AsyncTask.THREAD_POOL_EXECUTOR is not getting called.

推荐答案

谢谢 Stallion 和 royB 重播....我找到了导致问题的原因...

Hi thanku Stallion and royB for replay....i found what was causing the problem...

多个异步任务的标准如下,

criteria for multiple AsynC task is as below,

甜甜圈之前(直到 1.6 版本)

There was no concept of Multiple AsyncTask.

从甜甜圈到蜂窝(1.6 – 3.0)

Here it uses multiple AsyncTask in parallel by default and you cannot customize it.

Honeycomb 直到 Jelly Bean(版本 3.0 – 4.3.1)

private static final int CORE_POOL_SIZE = 5;
private static final int MAXIMUM_POOL_SIZE = 128;
private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(10);

来自奇巧(4.4 以上):

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 BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(128);

当我在 Android 版本:4.4.3 上运行时,我使用了 Runtime.getRuntime().availableProcessors();在我的日志中打印 CPU_COUNT 它显示 2 所以 2+1=3 是我设备的 CORE_POOL_SIZE,正如我从下面的工作室调试器控制台中看到的,

As i am running on Android version: 4.4.3 ,i used Runtime.getRuntime().availableProcessors(); to print CPU_COUNT in my logs it showed 2 so 2+1=3 is the CORE_POOL_SIZE for my device, as i can see from studio debugger console below,

同时只有 3 个线程可以在我的设备中运行,因为这些线程已经被占用(正在运行)......因为我在 doInbackground() 方法中使用 while 循环,这些异步任务永远不会完成,所以新的 AsyncTask 将进入队列并且没有让线程执行.现在我已经确保异步任务在工作结束后立即正确终止.在我修复之后,您可以在下面看到异步任务(线程)可用(处于等待状态)来执行新任务...

simultaneously only 3 threads can run in my device and as those threads were already occupied(running).. as i was using while loop in doInbackground() method, those Async Task was never getting finished so new AsyncTask will go to queue and was not getting the Thread to execute. now i have made sure that Async Task are properly terminated as soon as its work is over. and after my fix u can see below that Async Tasks(Threads) are available(in wait state) to take up the new Task...

这篇关于理解android中的并行异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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