文章詳目資料

資訊電子學刊

  • 加入收藏
  • 下載文章
篇名 Android 執行緒特性及應用之探討
卷期 7:1
並列篇名 A survey of characteristics and usage of Android Thread
作者 俎鴻明呂光裕王秋光
頁次 017-030
關鍵字 LooperHandlerAsyncTask
出刊日期 201703

中文摘要

傳統的 Java 平台並不是針對某種特定設備而設計的平台,他是一種跨設備、跨作業系統的程式開發平 台,而Android 系統主要是用來支援行動設備而開發的作業系統。基于行動設備是一種client 端的設備,因 此行動設備的應用程式着重在和使用者的直接互動(透過GUI 介面)。這兩種平台基本定位的不同也造成 Android 應用程式的主執行緒(通常稱為UI 執行緒)和Java 應用程式的主執行緒在功能定位及特性上的不同。 Android 應用程式的主執行緒被系統(Dalvik VM)賦予的主要功能是負責GUI 的建構、使用者的介面操作及介 面更新等GUI 相關的工作,相反的,Java 應用程式的主執行緒並未被系統(JVM)賦予GUI 相關的工作,如果 Java 應用程式內有使用AWT 或Swing GUI 介面時,JVM 會替程式再建立多個執行緒來專門負責GUI 相關 的工作,主執行緒只須專注自己本身的工作即可,不須直接執行GUI 相關的工作。 程式設計師如果不瞭解這兩個平台執行緒的差異,仍舊完全使用Java 程式設計的思維來設計Android 應用程式時往往會碰到一些問題。最常碰到的就是典型的ANR(Application Not Responding)問題。另外一個 常見的問題是當程式設計師試圖使用自行建立的執行緒來更新GUI 元件時,程式就會莫名的被系統終止執 行。而當主執行緒必需和程式內的其他執行緒互傳訊息時,如果使用Java 平台原有的IPC 訊息傳送機制時 (Inter-Process Communication)也可能會造成ANR 的問題。 Android 平台保留了絕大部份Java 平台的執行緒相關技術及功能,Android 也特別針對自己的特性提供 了一些執行緒的相關技術及支援。本論文先簡單的探討復習Java 平台執行緒相關的功能技術,接著探討 Android UI 執行緒的特性,最後探討Android 執行緒訊息傳遞機制(Looper、Handler、MessageQueue)、 HandlerThread、IntentService 及AsyncTask 等技術的特性及使用方法。相信讀者在讀完本篇論文後,在設計 Android 應用程式時應該可以避免上述所提的常見錯誤,除了節省程式開發及除錯的時間外,也能使用上述 探討的技術設計出正確、高效能的Android 應用程式。

英文摘要

Java platform is not designed for a specific device, it is a cross-device, cross-operating system platform for general application development. Android platform, on the other end, is an operating system mainly designed to support mobile devices. Since mobile devices are client-side devices, applications for mobile devices lay emphasis on direct interaction with users through GUI. The different orientation between these two platforms also contributes to the differences of functionality and characteristics between the main thread of application for these two platforms. The main task given by the Dalvik VM for the main thread of Android application is GUI-related operations. On the contrary, JVM does not assign GUI-related work to the main thread of Java application. If AWT or Swing GUI is used inside Java application, JVM will create more threads that are specifically responsible for GUI-related work, the main thread can focus on its own work without performing GUI-related work directly. When designing an Android application, it tends to encounter some problems if programmers do not understand the differences mentioned above and remains totally the thinking of Java thread programming. The most commonly encountered problem is the typical ANR (Application Not Responding) problem. Another commonly encountered problem is program been inexplicably terminated when updating GUI elements by other threads than the main thread. And, if main thread communicates with other threads by java IPC mechanisms, it may also cause ANR problem. In addition to the original Thread’s technologies and features supported by Java platform, Android also, according to the characteristics of its own, provides some thread-related technologies and supports. This paper first briefly reviews the relevant thread technologies offered by Java, then discusses properties of Android UI thread(i.e. main thread), and finally explores the characteristics and usage of Android Thread message passing mechanism (Looper, Handler, MessageQueue), HandlerThread, IntentService and AsyncTask, etc.,. We believe that, after reading this paper, programmers should be able to avoid common mistakes mentioned above, and, in addition to saving program development and debug time, programmers are expected to design correct and high-performance Android applications using thread-related technologies explored in this paper.

關鍵知識WIKI

相關文獻