[转]runOnUiThread 、 Handler 对比(一)

 

 

 
[java] view plaincopy
 
  1. this.runOnUiThread(new Runnable() {  
  2.         @Override  
  3.         public void run() {  
  4.             try {  
  5.                 Thread.sleep(1000 * 5);  
  6.             } catch (InterruptedException e) {  
  7.                 e.printStackTrace();  
  8.             }  
  9.                  InputMethodUtils.show(context, editText);    
  10.                  System.out.println("xx");  
  11.             }  
  12.         });  
  13.         System.out.println("xxxxxx");  

在onCreate中执行改方法,。执行结果如下

xx

xxxxxxx

想想也应该明白, 系统只有一个(唯一)独立的Main线程--UIThread, 所有跟界面相关的都是同步执行

 

[java] view plaincopy
 
  1.     new Handler().post(new Runnable() {  
  2.    @Override  
  3.    public void run() {  
  4.     try {  
  5.      Thread.sleep(1000 * 5);  
  6.     } catch (InterruptedException e) {  
  7.      e.printStackTrace();  
  8.     }  
  9.     InputMethodUtils.show(context, editText);  
  10.     System.out.println("xx");  
  11.    }  
  12.   });  
  13.   System.out.println("xxxxxx");  

在onCreate中执行改方法,。执行结果如下

xxxxxxx

xx

先结束了onCreate 然后handler中的线程仍然继续执行

posted @ 2015-11-03 18:23  yunlvrensheng  阅读(265)  评论(0编辑  收藏  举报