【问题标题】:Interesting thread behavior when Layout Inflater inflates EditTextLayout Inflater 膨胀 EditText 时有趣的线程行为
【发布时间】:2015-07-29 23:34:24
【问题描述】:

通过设备管理器观看我的应用时,我发现了充气器的行为,我希望堆栈溢出社区中的某个人可以帮助我理解。我有一个片段,当它使用布局充气器充气 EditText 时,它会产生两个异步任务。为什么会产生这些,我能做些什么来防止这种情况发生或事后关闭它们?

当我注释掉 EditText 时,在片段加载后我看到了这个:

放回去之后:

我的片段看起来像:

public class SearchFragment implements View.OnClickListener{

    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String TAG = "HistorySearchFragment";

    private static final String ARG_SECTION_NUMBER = "sectionNumber";

    public static SearchFragment newInstance(int sectionNumber) {
        SearchFragment fragment = new SearchFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_search, container, false)

        return view;
    }
}

fragment_search.xml 看起来像

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context="com.main.SearchFragment"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/search_button_bw"
    android:layout_below="@+id/space"
    android:isScrollContainer="false">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

/*************************************************
THIS TEXT BOX HERE
/************************************************/
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

/*************************************************
To here
/************************************************/

    </RelativeLayout>
</ScrollView >



<Button
    android:id="@+id/search_button_bw"
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:text="Search"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

我已经证实,这只发生在这个片段加载并且任务每次都没有失败时产生。有谁知道为什么会发生这种情况以及如何预防?

【问题讨论】:

    标签: android android-layout android-fragments android-edittext android-inflate


    【解决方案1】:

    有谁知道为什么会这样

    TextView 具有 references to AsyncTask in its locale handling(至少在其 Android 5.1 版本中;您需要检查您正在运行的特定 Android 版本)。

    AsyncTask will initialize a thread pool when the AsyncTask class is loaded 作为设置其余 static 数据成员的一部分。该池的最小池大小为 CPU 核心数加一。因此,在模拟器或单核硬件上,一旦引用了AsyncTask 类,就会创建两个Thread 对象。它们将在您的整个过程中存在。请注意,在多核 CPU 上,我希望更多线程在此池中处于稳定状态,并且如果有很多任务处于活动状态,则可能会有更多线程(内核数量最多加一倍)。

    如何预防?

    不要使用TextView 或任何继承自它的东西。此外,不要使用任何其他引用 AsyncTask 的框架类。

    由于这些线程没有运行(正如您从屏幕截图中看到的那样),我看不出您的问题是什么。如果有的话,我们需要在框架中使用后台线程更多东西,因为在某些地方,Android 的框架代码会与StrictMode 线程检查发生冲突。

    我该怎么做才能...之后关闭它们?

    它们是关闭的,只要它们没有运行。正如您在屏幕截图中看到的那样,它们被阻止(“等待”)。它们将保持该状态,直到需要运行任务为止。这就是线程池的工作原理。

    【讨论】:

    • 这对我来说没有意义。为什么其他片段中的每个其他编辑文本都不会创建异步任务,而只会创建这个。至于问题。我们在一个资源非常受限的设备上,不想在不需要的时候创建额外的资源。我们还有其他后台线程旨在减轻主线程的负载。
    • @nbroeking:“为什么其他片段中的每个其他编辑文本都不会创建异步任务,而只是创建这个”——我不知道。也许语言环境逻辑只在这个上执行,所以这是第一次引用 AsyncTask 并加载它的类。也许您正在运行除 Android 5.1 以外的其他东西(因为我的链接指向 master 分支,目前是 5.1)并且您在较旧的 Android 版本上遇到了其他一些 AsyncTask 行为。您进行测试的方式可能存在问题。
    • @nbroeking:但是,正如我所指出的,AsyncTask 在框架代码的许多地方都使用了。您可能无法避免加载此类。您碰巧基于此EditText 加载AsyncTask,但将来它可能会从您使用的其他东西加载。如果您正在为此“资源受限设备”构建自己的自定义 ROM,您可以将 AsyncTask 上的 CORE_POOL_SIZE 更改为零或其他值,然后看看它是如何工作的。
    • 我们实际使用的是Android 4.2.2。但我会给你一个投票,让我指出方向,当我可以验证这是创建 AsyncTasks 的原因时,我会接受你的回答。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2014-05-18
    • 2023-03-14
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多