对Android中handler处理两个线程的理解


我看了网上的教程是这样的,用handler除了一个线程,就是控制progressbar。于是我拓展了一下,我另外写了个计数的线程。先看源代码。

  1. public class HandlerTestActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.       
  4.     private ProgressBar pbar=null;  
  5.     private Button startBtn = null;  
  6.     private Button endBtn = null;  
  7.       
  8.     private TextView timerView = null;  
  9.     private Button endtimer = null;  
  10.       
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.main);  
  15.           
  16.         pbar = (ProgressBar)findViewById(R.id.pbar);  
  17.         pbar.setMax(100);  
  18.           
  19.         startBtn = (Button)findViewById(R.id.startbtn);  
  20.         endBtn = (Button)findViewById(R.id.endbtn);  
  21.         timerView = (TextView)findViewById(R.id.timerView);  
  22.         endtimer = (Button)findViewById(R.id.endtimer);  
  23.           
  24.         startBtn.setText("开始");  
  25.         startBtn.setOnClickListener(new OnClickListener(){  
  26.             public void onClick(View v) {  
  27.                 pbar.setVisibility(View.VISIBLE);  
  28.                 handler.post(runnable);  
  29.                 handler.post(timerRunnabler);  
  30.             }  
  31.         });  
  32.         endBtn.setText("结束");  
  33.         endBtn.setOnClickListener(new OnClickListener(){  
  34.             public void onClick(View v) {  
  35.                 pbar.setProgress(0);  
  36.                 handler.removeCallbacks(runnable);  
  37.             }  
  38.         });  
  39.           
  40.         endtimer.setOnClickListener(new OnClickListener(){  
  41.             public void onClick(View v) {  
  42.                 handler.removeCallbacks(timerRunnabler);  
  43.             }  
  44.         });  
  45.     }  
  46.       
  47.       
  48.     Handler handler = new Handler(){  
  49.         @Override  
  50.         public void handleMessage(Message msg){  
  51.             int intmsg = msg.what;  
  52.             switch(intmsg){  
  53.                 case 0:  
  54.                     pbar.setProgress(msg.arg1);  
  55.                     if(msg.arg1<pbar.getMax()){  
  56.                         handler.post(runnable);       
  57.                     }else{  
  58.                         handler.removeCallbacks(runnable);  
  59.                     }  
  60.                     break;  
  61.                 case 1:  
  62.                     timerView.setText("倒计时"+msg.arg1);  
  63.                     handler.post(timerRunnabler);  
  64.                     break;  
  65.             }  
  66.         }  
  67.     };   
  68.     Runnable runnable = new Runnable(){  
  69.         int i = 0;  
  70.         public void run(){  
  71.             i= i+ 5;  
  72.             Message msg=new Message();  
  73.             msg.arg1=i;  
  74.             msg.what=0;  
  75.             Log.d("System.out","i="+i+"----runnableId="+Thread.currentThread().getId()+"|----runnableName="+Thread.currentThread().getName());  
  76.             try{  
  77.                 Thread.sleep(3000);   
  78.             }catch(InterruptedException e){  
  79.                 e.printStackTrace();  
  80.             }  
  81.             handler.sendMessage(msg);  
  82.               
  83.             if(i==80){  
  84.                 pbar.setProgress(0);  
  85.                 handler.removeCallbacks(runnable);  
  86.                 //handler.removeMessages(0);   
  87.             }  
  88.         }  
  89.     };  
  90.       
  91.     //计数器   
  92.     Runnable timerRunnabler = new Runnable(){  
  93.   
  94.         int j=0;  
  95.         public void run() {  
  96.             j=j+1;  
  97.             Message msg = new Message();  
  98.             msg.arg1=j;  
  99.             msg.what=1;  
  100.             Log.d("System.out","j="+j+"----timerRunnablerId="+Thread.currentThread().getId()+"|----timerRunnablerName="+Thread.currentThread().getName());  
  101.   
  102.             try{  
  103.                 Thread.sleep(1000);  
  104.             }catch(InterruptedException e){  
  105.                 e.printStackTrace();  
  106.             }  
  107.             handler.sendMessage(msg);  
  108.         }  
  109.     };  
  110. }  

操作界面是这样的

我点击“开始”,运行runnabletimerRunnabler,,progressbar和倒计时开始进行。是设置runnable执行sleep,3秒后执行,timerRunnabler执行sleep,1秒后执行,但是从执行的效果上看是同步的,我的想法应该是不同步的。

我在logcat中看到了,他们执行是按顺序来的,也就是进入了线程队列里了。两者应该是独立的,为什么会同步执行呢

然后我点击“结束”,我让下面的倒计时任然继续。然后timerRunnabler执行是正常的,1秒一执行。

于是我的问题:

1、当点击"结束"按钮时,progressbar的progress没有归0,比如是50,点击"开始"时候,又在50的基础上开始输出,比如输出55
2、我这里是2个线程,runnable和timerRunnabler,为什么打印出当前线程的名字,是同一个,main
3、两个线程,runnable和timerRunnabler,执行的时间安排是,runnable:2000,timerRunnabler:1000,但实际执行的时候好像是同时执行的
 progressbar增加一格,textview计数增加1,而点击"结束",progressbar不再执行时,timerRunnabler执行是1秒一执行。
 

请赐教啊

handler的作用我理解是这样的,看图

相关内容