在UI线程加载和创建的AsyncTask之间的区别是什么? [英] What is the difference between loading and creating AsyncTask on the UI thread?

查看:119
本文介绍了在UI线程加载和创建的AsyncTask之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读 AsyncTask的文档,对线程规则​​的一部分,我发现这一点:

While reading AsyncTask documentation, the part on Threading rules, I found this:

      
  • 的AsyncTask的类必须在UI线程加载。这样做是   自动为JELLY_BEAN的。
  •   
  • 任务实例必须创建   在UI线程上。
  •   
  • 执行(参数...)必须在UI线程上被调用。
  •   
  • The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.
  • The task instance must be created on the UI thread.
  • execute(Params...) must be invoked on the UI thread.

什么是加载的意思?这不是实例或执行,如文档谈到了这些后。

What is meant by "loading"? It's not instantiating or executing, as the documentation talked about those later.

推荐答案

我不相信这个答案是其实正确的。

I don't believe that this answer is actually correct.

这是没有意义的文件为列表中的实例加载的,如果这些事情其实是一样的。我认为这种说法

It wouldn't make sense for the documentation to separately list instantiation and loading, if those things were actually the same. I believe this statement

在AsyncTask的类必须在UI线程加载。

The AsyncTask class must be loaded on the UI thread.

指的是 Java类加载。换句话说,在的AsyncTask 本身需要在主线程加载。在果冻豆(或更高版本),这是自动的。但是,在旧版本的Andr​​oid,对于这个类要在另一个线程,这可能会导致问题加载的电位

is referring to Java Class Loading. In other words, the AsyncTask class itself needs to be loaded on the main thread. In Jelly Bean (or later), this is automatic. But, in older versions of Android, there is the potential for this class to be loaded on another thread, which can cause problems.

有关更多信息,请参见本谷歌讨论。基本上,还有条件(例如,使用code IntentService ),可以使的AsyncTask 是第一加载的错误(无主)线程。

See this Google discussion for more information. Basically, there are conditions (for example, code using IntentService) that can cause the AsyncTask to be first loaded on the wrong (non-main) thread.

这样做的简单的解决办法之前,果冻豆,似乎是使用这样的:

The simplest fix for this, prior to Jelly Bean, seems to be to use something like:

Class.forName("android.os.AsyncTask");

在应用程序的的onCreate()方法,给力类当你希望它加载的情况发生。

in the Application's onCreate() method, to force class loading to happen when you want it to.

创建的AsyncTask 实例可能是你认为它是...实例吧:

Creating the AsyncTask instance is probably what you think it is ... instantiating it:

MyAsyncTask task = new MyAsyncTask();

和那也应该在主线程上运行。

and that should also be run on the main thread.

这篇关于在UI线程加载和创建的AsyncTask之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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