日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網(wǎng)站、值得信賴(lài)的軟件下載站!

      首頁(yè)編程開(kāi)發(fā)Android → Android 多線程處理之多線程用法大集合

      Android 多線程處理之多線程用法大集合

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:西西整理時(shí)間:2012/11/23 22:06:51字體大。A-A+

      作者:西西點(diǎn)擊:47次評(píng)論:0次標(biāo)簽: 多線程

      • 類(lèi)型:服務(wù)器區(qū)大。21KB語(yǔ)言:中文 評(píng)分:6.6
      • 標(biāo)簽:
      立即下載

      handler.post(r)其實(shí)這樣并不會(huì)新起線程,只是執(zhí)行的runnable里的run()方法,卻沒(méi)有執(zhí)行start()方法,所以runnable走的還是UI線程。

      1.如果像這樣,是可以操作ui,但是run還是走在主線程,見(jiàn)打印出來(lái)的Log線程名字是main,說(shuō)明是主線程。

      這就是為什么可以直接在run方法里操作ui,因?yàn)樗举|(zhì)還是ui線程

      handler.post(new Runnable(){

        public void run(){

        Log.e("當(dāng)前線程:",Thread.currrentThread.getName());//這里打印de結(jié)果會(huì)是main

        setTitle("哈哈");

            }

      });

      2.通過(guò)HandlerThread獲取到looper卻是可以新起線程,但是在這里的run方法里操作ui是不可能的,但是這顯然有個(gè)缺點(diǎn),如果你執(zhí)行多次post(r)方法其實(shí)走的還是HandlerThread線程。假如你執(zhí)行5次,n次,其實(shí)還是一次并且它們是串行的。假如下載5張圖片,你會(huì)看到圖片是下完第一張,才會(huì)去下第二張。

      實(shí)踐證明,只有是擁有主線程looper的handler才可以操作ui,而在主線程操作ui可以在handler的handlerMessage()方法中操作Ui,也可以在handler的post(r)的run方法里操作Ui.

      HandlerThread ht = new HandlerThread("handler thread");

      ht.start();

      handler = new Handler(ht.getLooper());

      handler.post(new Runnable(){//這里run()方法其實(shí)還是在等ht.start()調(diào)用

        public void run(){

        Log.e("當(dāng)前線程:",Thread.currrentThread.getName());//這里打印的會(huì)是handler thread

        setTitle("哈哈");//這樣必定報(bào)錯(cuò)

        //android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

            }

      });

      這樣該怎么辦呢,呵呵,可以無(wú)參構(gòu)建一個(gè)handler。用這個(gè)handler來(lái)發(fā)送消息和處理消息,用上面的handler來(lái)開(kāi)啟新線程。

      mainHandler = new Handler(){

        protecket void handlerMessage(Message msg){

          setTitle("哈哈");//這樣就不會(huì)報(bào)錯(cuò)啦

        }

      }

      handler.post(new Runnable(){//這里run()方法其實(shí)還是在等ht.start()調(diào)用

        public void run(){

        Log.e("當(dāng)前線程:",Thread.currrentThread.getName());//這里打印的會(huì)是handler thread

        mainHandler.sendEmpetMessage();//用mainHandler來(lái)發(fā)送消息

        //setTitle("哈哈");//這樣必定報(bào)錯(cuò)

        //android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

            }

      });

      打印Log:

      3.其實(shí)第2個(gè)方法顯得麻煩而且低效,用了2個(gè)handler,一個(gè)用來(lái)發(fā)起線程,一個(gè)用于處理消息。發(fā)起線程的handler必須擁有l(wèi)ooper,所以還要實(shí)例化一個(gè)HanderThread;而處理消息的handler則不需要looper,因?yàn)樗J(rèn)擁有主線程的looper,所以可以在這個(gè)handler處理ui。

      其實(shí)可以只需要實(shí)例化一個(gè)handler,在主線程里構(gòu)建一個(gè)無(wú)參的handler,然后由它發(fā)送和處理消息。而創(chuàng)建線程的任務(wù)就不用handler了,直接用new Thread(r).start();然后在r的run()方法里面處理邏輯事務(wù)。

      用這樣的模式下載5張圖片,你就可能不會(huì)看到圖片一張挨著一張展示出來(lái),可能第2張先出來(lái),也可能同時(shí)出來(lái)3張,5條線程很隨機(jī)的。

      private void loadImagesByThread(final String url,final int id){//通過(guò)Thread來(lái)new 出多個(gè)線程
             
              new Thread(new Runnable(){

                  @Override
                  public void run() {
                      // TODO Auto-generated method stub
                      Log.e("當(dāng)前線程:", ""+Thread.currentThread().getName());
                      Drawable drawable = null;
                      try {
                          drawable = Drawable.createFromStream(new URL(url).openStream(), "image.gif");
                      } catch (MalformedURLException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      } catch (IOException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                      }
                      Message msg = mainHandler.obtainMessage();
                      msg.what = 2012;
                      msg.arg1 = id;
                      msg.obj = drawable;
                      msg.sendToTarget();
                     
                  }
                 
              }).start();
          }

      打印Log:

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過(guò)難過(guò)
        • 5 囧
        • 3 圍觀圍觀
        • 2 無(wú)聊無(wú)聊

        熱門(mén)評(píng)論

        最新評(píng)論

        發(fā)表評(píng)論 查看所有評(píng)論(0)

        昵稱(chēng):
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)