1. GUI线程框架

常见的 Swing, SWT框架都是作为单线程子系统来实现的,实际上不仅限于在Java中, Qt、MacOS Cocoa以及其他的环境中的GUI框架都是单线程的。虽然很多人尝试过其他的方案但最终回到了单线程的事件模型,简单来看最主要的原因还在于多线程的GUI中更容易出现死锁的问题[1]。

从android应用层来看,UI线程可以对应到系统中的ActivityThread,其中的main方法,建立主线程关联的 Looper 以及MessageQueue。不难看出它是应用层的入口点。

     public static void main(String[] args) {
SamplingProfilerIntegration.start(); // CloseGuard defaults to true and can be quite spammy. We
// disable it here, but selectively enable it later (via
// StrictMode) on debug builds, but using DropBox, not logs.
CloseGuard.setEnabled(false); Process.setArgV0("<pre-initialized>"); Looper.prepareMainLooper();
if (sMainThreadHandler == null) {
sMainThreadHandler = new Handler();
} ActivityThread thread = new ActivityThread();
thread.attach(false); if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
} Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited");
}

2. ANR

恶名远播的“Application Not Responding” 弹出框。由1中所述,单线程模型下的主线程如果做了其它大量复杂耗时的工作,无疑会对应用的性能造成极大影响。看到ANR实际上是系统发出警告了。在Activity生命周期方法里面的代码处理,BroadcastReceiver#onReceive()的执行,同一进程级内的直接运行在主线程中的Service 在执行复杂后台任务时, 以及对IO的操作诸如数据库访问、文件读写等都需要考虑耗时的情况,必要时采用异步执行。

除了手动构建新的Thread方式外,系统也提供了众多方便的工具,如 AsyncTaskHandlerThreadLoaderManager 等等。我们完全可以将主线程上的重负分离出来,构建更具有良好交互响应的应用。

3. 多线程条件下的影响

注意到多线程执行的环境下,自定义实现的Thread需要防止与主线程产生竞争的问题。系统底层线程(Linux 的POSIX threads,又称 Pthreads)在被调度执行时与线程自身的优先级有关了。而一般自定义实现的Thread优先级别与主线程相同,考虑到对CPU时间片的占用,我们可以降低这类工作线程的优先级:

"Standard priority background threads. This gives your thread a slightly lower than normal priority, so that it will have less chance of impacting the responsiveness of the user interface."
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

对比可以看到 android.os.AsyncTask 中新增异步任务时的实现部分 :

 mWorker = new WorkerRunnable<Params, Result>() {
public Result call() throws Exception {
mTaskInvoked.set(true); Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
return postResult(doInBackground(mParams));
}
};

如果后台有工作线程频繁的执行操作时,在单核CPU的设备上对比可以发现,优先级的设置对主线程有比较明显的响应。

引用:

[1] 《Java并发编程实践》 P156

有关android UI 线程的更多相关文章

  1. Android UI线程和非UI线程

    Android UI线程和非UI线程 UI线程及Android的单线程模型原则 当应用启动,系统会创建一个主线程(main thread). 这个主线程负责向UI组件分发事件(包括绘制事件),也是在这 ...

  2. android UI线程安全问题

    在Android的子线程去更新UI的内容,会导致不确定的异常. 因为Android有个模式是,单一线程模型:Android UI工具箱(toolkit)不是一个线程安全的,并且它总是被放在主线程上操作 ...

  3. android脚步---如何看log之程序停止运行,和UI线程和非UI线程之间切换

    经常运行eclipse时,烧到手机出现,“停止运行”,这时候得通过logcat查log了.一般这种情况属于FATAL EXCEPTION,所以检索FATAL 或者 EXCEPTION,然后往下看几行 ...

  4. Android子线程真的不能更新UI么

    Android单线程模型是这样描述的: Android UI操作并不是线程安全的,并且这些操作必须在UI线程执行 如果在其它线程访问UI线程,Android提供了以下的方式: Activity.run ...

  5. Android子线程更新UI主线程方法之Handler

    背景: 我们开发应用程序的时候,处于线程安全的原因子线程通常是不能直接更新主线程(UI线程)中的UI元素的,那么在Android开发中有几种方法解决这个问题,其中方法之一就是利用Handler处理的. ...

  6. [原] Android performClick无效,UI线程理解

    原因 开发过程中遇到button.performClick()无效,原因是View.performClick()需要再UI线程中调用才会有效执行. 响应系统调用的方法(比如报告用户动作的onKeyDo ...

  7. 【Android开发学习笔记】【随笔】UI线程

    概念 UI线程 是Android中的主线程,涉及到UI方面的一些操作都需要在ui线程中进行操作 在非ui线程中想操作ui,就会报错 android.view.ViewRoot$CalledFromWr ...

  8. Android ActivityThread(主线程或UI线程)简介

    1. ActivityThread功能 它管理应用进程的主线程的执行(相当于普通Java程序的main入口函数),并根据AMS的要求(通过IApplicationThread接口,AMS为Client ...

  9. Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面

    Android应用的开发过程中需要把繁重的任务(IO,网络连接等)放到其他线程中异步执行,达到不阻塞UI的效果. 下面将由浅入深介绍Android进行异步处理的实现方法和系统底层的实现原理. 本文介绍 ...

随机推荐

  1. Parallel WebDriver executions using TestNG

    In this post, we will see how does one make use of TestNG to kick off parallel UI tests using WebDri ...

  2. poj 1845 Sumdiv (数论)

    题目链接 题意:求 A^B的所有约数之和对9901取模后的结果. 分析: 看了小优的博客写的. 分析来自 http://blog.csdn.net/lyy289065406/article/detai ...

  3. Android布局详解之一:FrameLayout

      原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...

  4. UVa 1638 (递推) Pole Arrangement

    很遗憾,这么好的一道题,自己没想出来,也许太心急了吧. 题意: 有长度为1.2.3...n的n个杆子排成一行.问从左到右看能看到l个杆子,从右往左看能看到r个杆子,有多少种排列方法. 分析: 设状态d ...

  5. [转:CSS3-前端] CSS3发光和多种图片处理

    原文链接:http://www.qianduan.net/css3-image-styles.html 一些上流的CSS3图片样式 神飞 发表于 24. Sep, 2011, 分类: CSS , 46 ...

  6. javascript实现继承的一种方式

    function extend(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototy ...

  7. nginx - ssl 配置 - globelsign ssl

    前提: 3个文件 - domain.csr.domain.key.xxx.cer 简述: 1. 本地生成 .key文件  [附件] 2. 再利用key文件,生成csr(certificate Secu ...

  8. Java [Leetcode 169]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  9. Java [leetcode 8] String to Integer (atoi)

    问题描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ...

  10. 22个所见即所得在线 Web 编辑器

    前言: 关于编辑器,适合的才是最好的,接下来,我会写一些关于日志编辑器的文章,今天就写写,可能内容会比较多. --------------------------------------------- ...