<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 閱讀(364) 評論(0)  編輯  收藏 所屬分類: Android Dev

    主站蜘蛛池模板: 综合一区自拍亚洲综合图区| 亚洲最新永久在线观看| 亚洲综合在线一区二区三区| 国产精品成人免费福利| 中文字幕在线观看亚洲| h视频在线观看免费网站| 在线免费观看亚洲| 91免费在线播放| 亚洲国产一区在线观看| 人妻视频一区二区三区免费| 国产成人精品日本亚洲直接| 毛片a级毛片免费播放下载| 亚洲欧美日韩中文高清www777| 免费黄色小视频网站| 国产91成人精品亚洲精品| 亚洲精品国产电影| A毛片毛片看免费| 久久精品国产精品亚洲蜜月| 久久久久久AV无码免费网站 | 欧洲精品99毛片免费高清观看| 亚洲AV无码乱码在线观看裸奔| 精品国产麻豆免费人成网站| 久久精品亚洲中文字幕无码网站| 91高清免费国产自产拍2021| 亚洲国产精品美女| 无码国模国产在线观看免费| 成人嫩草影院免费观看| 久久亚洲国产伦理| 91黑丝国产线观看免费| 无码一区二区三区亚洲人妻| 永久亚洲成a人片777777| 久久综合给合久久国产免费 | 久草免费福利资源站| 亚洲人成电影在线观看青青| 国产午夜免费福利红片| a级毛片毛片免费观看永久| 亚洲精品中文字幕麻豆 | 久久亚洲中文字幕精品一区四| 精品亚洲永久免费精品 | 亚洲中文字幕视频国产| 日韩精品无码一区二区三区免费|