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

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

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

    統(tǒng)計

    留言簿(1)

    DB

    Others

    QA

    Tech Website

    閱讀排行榜

    評論排行榜

    【轉】Painless threading

    寫在前面:寫Android程序有一個很重要的原則,不阻塞UI線程。因此Android提供了5種方法來,讓一些耗時的作業(yè)在其它線程中執(zhí)行,然后把結果返回給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 閱讀(359) 評論(0)  編輯  收藏 所屬分類: Android Dev

    主站蜘蛛池模板: av午夜福利一片免费看久久| 久久大香香蕉国产免费网站 | 亚洲成av人影院| 青青操免费在线观看| 亚洲日本在线播放| 又黄又大又爽免费视频| 久久久久国产精品免费看| 成人亚洲国产va天堂| 国产亚洲美女精品久久久| 成人免费观看一区二区| 搜日本一区二区三区免费高清视频| 亚洲视频在线免费看| 亚洲人成人伊人成综合网无码| 中文字幕精品亚洲无线码一区应用| 亚洲砖码砖专无区2023| 亚洲黄黄黄网站在线观看| 国产高清免费视频| jizz免费一区二区三区| 中文字幕亚洲码在线| 亚洲成a人片在线观看无码专区 | 永久免费AV无码国产网站| 日韩毛片在线免费观看| 亚洲国产美女精品久久久久| 国产偷窥女洗浴在线观看亚洲 | 亚洲欧美国产国产一区二区三区 | 全免费a级毛片免费看不卡| 国产美女视频免费观看的网站| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 无码人妻一区二区三区免费 | 免费无码一区二区三区蜜桃 | 国产久爱免费精品视频| 亚洲日韩国产AV无码无码精品| 免费看片A级毛片免费看| 99爱在线精品视频免费观看9| 亚洲欧洲春色校园另类小说| 黑人大战亚洲人精品一区| 国产成人免费福利网站| 91精品免费久久久久久久久| a级成人免费毛片完整版| 一级毛片高清免费播放| 亚洲AV永久无码精品网站在线观看|