在调用AsyncTask :: execute之后,AsyncTask的doInBackground开始执行为时已晚. [英] AsyncTask's doInBackground starts its execution too late after AsyncTask::execute is called

查看:110
本文介绍了在调用AsyncTask :: execute之后,AsyncTask的doInBackground开始执行为时已晚.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个AsyncTask,在大多数情况下,在调用其构造函数和调用其doInBackground之间没有延迟(延迟为0毫秒). 但是,每当后台进行联系人同步时,我经常会在AsyncTasks的构造函数和doInBackground之间遇到1-3秒的延迟.在我的情况下,这种延迟是无法接受的. 我知道AsyncTask是后台线程,可以通过使用Thread并将其优先级设置更高来解决此问题.但是我想发现的是,我怎么知道是什么导致我的AsyncTask的doInBackground被调用? 我使用了adb shell top -m 10,发生此问题时进程使用情况似乎很正常.

I wrote an AsyncTask and most of the time there is no delay between its constructor been called and its doInBackground been called (0 ms delay). But whenever contacts syncing is happening at the background, I often experience 1-3 seconds delay between my AsyncTasks's constructor and doInBackground. This delay is unacceptable in my circumstances. I understand that AsyncTask is a background thread and this problem can be solved by using Thread and setting its priority higher. But what I want to found out is, how do I know what's causing my AsyncTask's doInBackground from being called? I used adb shell top -m 10 and the process usage seems quite normal when this issue happened.

感谢您的帮助.

谢谢

推荐答案

我也长期面对此问题,但现在已解决.使用下面的代码

I also face this issue for long period, but now it is solved. Use the code below

new AsyncTaskName().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

代替代码

new AsyncTaskName().execute();

它可以解决延迟运行doInbackground的问题.

it would solve the problem of running doInbackground late.

这篇关于在调用AsyncTask :: execute之后,AsyncTask的doInBackground开始执行为时已晚.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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