<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

    閱讀排行榜

    評論排行榜

    【轉(zhuǎn)】Painless threading

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

    主站蜘蛛池模板: 1000部羞羞禁止免费观看视频 | 日本免费一区二区三区四区五六区| 亚洲国产精品综合久久网络| 污污的视频在线免费观看| 国产乱色精品成人免费视频| 成人精品国产亚洲欧洲| 亚洲欧洲日产国码一级毛片| 国产va免费精品| 亚洲av中文无码乱人伦在线咪咕| 免费无遮挡无码永久视频| 亚洲国产日韩在线人成下载| 在线观看人成视频免费| 青草青草视频2免费观看| 最新国产AV无码专区亚洲| 免费黄网站在线看| 精品亚洲成A人无码成A在线观看| 国产又黄又爽又猛的免费视频播放| 有码人妻在线免费看片| 亚洲AV无码一区东京热| 成人免费视频77777| 深夜特黄a级毛片免费播放| 国产亚洲一区二区三区在线观看| 最近高清中文字幕免费| 欧美色欧美亚洲另类二区| 亚洲国产三级在线观看| 免费h片在线观看网址最新| 亚洲精品国产av成拍色拍| 亚洲欭美日韩颜射在线二| 91短视频免费在线观看| 激情无码亚洲一区二区三区| 国产精品亚洲一区二区三区在线 | 亚洲成A∨人片天堂网无码| 无码一区二区三区免费| 亚洲色大18成人网站WWW在线播放| 亚洲人妻av伦理| 1000部夫妻午夜免费| 理论片在线观看免费| 久久精品国产亚洲av高清漫画| 卡1卡2卡3卡4卡5免费视频| 免费看无码特级毛片| 亚洲色偷偷偷综合网|