<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    統計

    留言簿(1)

    DB

    Others

    QA

    Tech Website

    閱讀排行榜

    評論排行榜

    【轉】Painless threading

    寫在前面:寫Android程序有一個很重要的原則,不阻塞UI線程。因此Android提供了5種方法來,讓一些耗時的作業在其它線程中執行,然后把結果返回給UI線程,以免阻塞UI線程。

    --------------------

    Whenever you first start an Android application, a thread called "main" is automatically created. The main thread, also called the UI thread, is very important because it is in charge of dispatching the events to the appropriate widgets and this includes the drawing events. It is also the thread you interact with Android widgets on. For instance, if you touch the a button on screen, the UI thread dispatches the touch event to the widget which in turn sets its pressed state and posts an invalidate request to the event queue. The UI thread dequeues the request and notifies the widget to redraw itself.

    This single thread model can yield poor performance in Android applications that do not consider the implications. Since everything happens on a single thread performing long operations, like network access or database queries, on this thread will block the whole user interface. No event can be dispatched, including drawing events, while the long operation is underway. From the user's perspective, the application appears hung. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.

    If you want to see how bad this can look, write a simple application with a button that invokes Thread.sleep(2000) in its OnClickListener. The button will remain in its pressed state for about 2 seconds before going back to its normal state. When this happens, it is very easy for the user to perceive the application as slow.

    Now that you know you must avoid lengthy operations on the UI thread, you will probably use extra threads (background or worker threads) to perform these operations, and rightly so. Let's take the example of a click listener downloading an image over the network and displaying it in an ImageView:

    public void onClick(View v) {
     
    new Thread(new Runnable() {
       
    public void run() {
         
    Bitmap b = loadImageFromNetwork();
          mImageView
    .setImageBitmap(b);
       
    }
     
    }).start();
    }

    At first, this code seems to be a good solution to your problem, as it does not block the UI thread. Unfortunately, it violates the single thread model: the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread. In this piece of code, the ImageView is manipulated on a worker thread, which can cause really weird problems. Tracking down and fixing such bugs can be difficult and time-consuming.

    Android offers several ways to access the UI thread from other threads. You may already be familiar with some of them but here is a comprehensive list:

    Any of these classes and methods could be used to correct our previous code example:

    public void onClick(View v) {
     
    new Thread(new Runnable() {
       
    public void run() {
         
    final Bitmap b = loadImageFromNetwork();
          mImageView
    .post(new Runnable() {
           
    public void run() {
              mImageView
    .setImageBitmap(b);
           
    }
         
    });
       
    }
     
    }).start();
    }

    Unfortunately, these classes and methods also tend to make your code more complicated and more difficult to read. It becomes even worse when your implement complex operations that require frequent UI updates. To remedy this problem, Android 1.5 offers a new utility class, called AsyncTask, that simplifies the creation of long-running tasks that need to communicate with the user interface.

    AsyncTask is also available for Android 1.0 and 1.1 under the name UserTask. It offers the exact same API and all you have to do is copy its source code in your application.

    The goal of AsyncTask is to take care of thread management for you. Our previous example can easily be rewritten with AsyncTask:

    public void onClick(View v) {
     
    new DownloadImageTask().execute("http://example.com/image.png");
    }

    private class DownloadImageTask extends AsyncTask {
         
    protected Bitmap doInBackground(String... urls) {
             
    return loadImageFromNetwork(urls[0]);
         
    }

         
    protected void onPostExecute(Bitmap result) {
             mImageView
    .setImageBitmap(result);
         
    }
     
    }

    As you can see, AsyncTask must be used by subclassing it. It is also very important to remember that an AsyncTask instance has to be created on the UI thread and can be executed only once. You can read the AsyncTask documentation for a full understanding on how to use this class, but here is a quick overview of how it works:

    In addition to the official documentation, you can read several complex examples in the source code of Shelves (ShelvesActivity.java andAddBookActivity.java) and Photostream (LoginActivity.javaPhotostreamActivity.java and ViewPhotoActivity.java). I highly recommend reading the source code of Shelves to see how to persist tasks across configuration changes and how to cancel them properly when the activity is destroyed.

    Regardless of whether or not you use AsyncTask, always remember these two rules about the single thread model: do not block the UI thread and make sure the Android UI toolkit is only accessed on the UI thread. AsyncTask just makes it easier to do both of these things.

    posted on 2011-08-23 09:25 XXXXXX 閱讀(363) 評論(0)  編輯  收藏 所屬分類: Android Dev

    主站蜘蛛池模板: 久久久亚洲精品国产| 亚洲中文字幕第一页在线| 日韩av无码久久精品免费| 99在线免费观看视频| 亚洲国产精品va在线播放| 亚洲高清不卡视频| 亚洲综合av一区二区三区不卡| 男女猛烈激情xx00免费视频| 久章草在线精品视频免费观看| 久久午夜夜伦鲁鲁片免费无码影视| 成人免费无码大片A毛片抽搐色欲| 亚洲天堂免费在线视频| 亚洲精品第一国产综合精品| 黄色a三级免费看| 亚洲免费视频观看| 亚洲真人日本在线| 久久免费高清视频| 国产精品另类激情久久久免费| 亚洲αv久久久噜噜噜噜噜| 羞羞视频在线免费观看| 亚洲男人的天堂一区二区| 亚洲另类自拍丝袜第1页| a级毛片免费全部播放无码| 暖暖免费高清日本一区二区三区| 337p日本欧洲亚洲大胆艺术| 成视频年人黄网站免费视频| 亚洲精品高清无码视频| 老司机午夜在线视频免费| 亚洲综合熟女久久久30p| 99ee6热久久免费精品6| 久久亚洲国产最新网站| 91精品视频免费| 亚洲视频在线不卡| 日韩a级毛片免费视频| 丝瓜app免费下载网址进入ios| 亚洲成AV人网址| 羞羞视频在线免费观看| 亚洲国产精品国自产电影| 久久久久免费精品国产小说| 亚洲精品无码av人在线观看| 成人免费黄色网址|