Handler+Thread和HandlerThread的用法比较

Handler+Thread

[java]  view plain copy
  1. package org.crazyit.handler;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Looper;  
  10. import android.os.Message;  
  11. import android.view.View;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. public class CalPrime1 extends Activity  
  16. {  
  17.     static final String UPPER_NUM = "upper";  
  18.     EditText etNum;  
  19.       
  20.     CalThread calThread;  
  21.     // 定义一个线程类  
  22.     class CalThread extends Thread  
  23.     {  
  24.         public Handler mHandler;  
  25.   
  26.         public void run()  
  27.         {  
  28.             //创建一个Looper对象  
  29.             Looper.prepare();  
  30.             mHandler = new MyHandler();  
  31.             //启动Looper对象  
  32.             Looper.loop();  
  33.         }  
  34.     }  
  35.       
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState)  
  38.     {  
  39.         super.onCreate(savedInstanceState);  
  40.         setContentView(R.layout.main);  
  41.         etNum = (EditText)findViewById(R.id.etNum);  
  42.         calThread = new CalThread();  
  43.         // 启动新线程  
  44.         calThread.start();  
  45.     }  
  46.       
  47.     public class MyHandler extends Handler{  
  48.           
  49.         @Override  
  50.         public void handleMessage(Message msg) {  
  51.             super.handleMessage(msg);  
  52.               
  53.             if(msg.what == 0x123)  
  54.             {  
  55.                 int upper = msg.getData().getInt(UPPER_NUM);  
  56.                 List<Integer> nums = new ArrayList<Integer>();  
  57.                 // 计算从2开始、到upper的所有质数  
  58.                 outer:  
  59.                 for (int i = 2 ; i <= upper ; i++)  
  60.                 {  
  61.                     // 用i处于从2开始、到i的平方根的所有数  
  62.                     for (int j = 2 ; j <= Math.sqrt(i) ; j++)  
  63.                     {  
  64.                         // 如果可以整除,表明这个数不是质数  
  65.                         if(i != 2 && i % j == 0)  
  66.                         {  
  67.                             continue outer;  
  68.                         }  
  69.                     }  
  70.                     nums.add(i);  
  71.                 }  
  72.                 // 使用Toast显示统计出来的所有质数  
  73.                 Toast.makeText(CalPrime1.this , nums.toString()  
  74.                     , Toast.LENGTH_LONG).show();  
  75.             }  
  76.         }  
  77.     }  
  78.       
  79.     // 为按钮的点击事件提供事件处理函数  
  80.     public void cal(View source)  
  81.     {  
  82.         // 创建消息  
  83.         Message msg = new Message();  
  84.         msg.what = 0x123;  
  85.         Bundle bundle = new Bundle();  
  86.         bundle.putInt(UPPER_NUM ,  
  87.             Integer.parseInt(etNum.getText().toString()));  
  88.         msg.setData(bundle);  
  89.         // 向新线程中的Handler发送消息  
  90.         calThread.mHandler.sendMessage(msg);  
  91.     }  
  92. }  

HandlerThread

[java]  view plain copy
  1. package org.crazyit.handler;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.HandlerThread;  
  10. import android.os.Looper;  
  11. import android.os.Message;  
  12. import android.view.View;  
  13. import android.widget.EditText;  
  14. import android.widget.Toast;  
  15.   
  16. /** 
  17.  * Description: 
  18.  * <br/>website: <a href="http://www.crazyit.org">crazyit.org</a> 
  19.  * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee 
  20.  * <br/>This program is protected by copyright laws. 
  21.  * <br/>Program Name: 
  22.  * <br/>Date: 
  23.  * @author Yeeku.H.Lee kongyeeku@163.com 
  24.  * @version 1.0 
  25.  */  
  26. public class CalPrime extends Activity  
  27. {  
  28.     static final String UPPER_NUM = "upper";  
  29.     EditText etNum;  
  30.       
  31.     Handler mHandler;  
  32.   
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState)  
  35.     {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.main);  
  38.         etNum = (EditText)findViewById(R.id.etNum);  
  39.           
  40.         HandlerThread handlerThread = new HandlerThread("SOHU");  
  41.         handlerThread.start();  
  42.           
  43.         mHandler = new MyHandler(handlerThread.getLooper());  
  44.     }  
  45.       
  46.     public class MyHandler extends Handler{  
  47.           
  48.         public MyHandler(){  
  49.             super();  
  50.         }  
  51.           
  52.         public MyHandler(Looper looper){  
  53.             super(looper);  
  54.         }  
  55.           
  56.         @Override  
  57.         public void handleMessage(Message msg) {  
  58.             super.handleMessage(msg);  
  59.               
  60.             if(msg.what == 0x123)  
  61.             {  
  62.                 int upper = msg.getData().getInt(UPPER_NUM);  
  63.                 List<Integer> nums = new ArrayList<Integer>();  
  64.                 // 计算从2开始、到upper的所有质数  
  65.                 outer:  
  66.                 for (int i = 2 ; i <= upper ; i++)  
  67.                 {  
  68.                     // 用i处于从2开始、到i的平方根的所有数  
  69.                     for (int j = 2 ; j <= Math.sqrt(i) ; j++)  
  70.                     {  
  71.                         // 如果可以整除,表明这个数不是质数  
  72.                         if(i != 2 && i % j == 0)  
  73.                         {  
  74.                             continue outer;  
  75.                         }  
  76.                     }  
  77.                     nums.add(i);  
  78.                 }  
  79.                 // 使用Toast显示统计出来的所有质数  
  80.                 Toast.makeText(CalPrime.this , nums.toString()  
  81.                     , Toast.LENGTH_LONG).show();  
  82.             }  
  83.         }  
  84.     }  
  85.       
  86.     // 为按钮的点击事件提供事件处理函数  
  87.     public void cal(View source)  
  88.     {  
  89.         // 创建消息  
  90.         Message msg = new Message();  
  91.         msg.what = 0x123;  
  92.         Bundle bundle = new Bundle();  
  93.         bundle.putInt(UPPER_NUM ,  
  94.             Integer.parseInt(etNum.getText().toString()));  
  95.         msg.setData(bundle);  
  96.         // 向新线程中的Handler发送消息  
  97.         mHandler.sendMessage(msg);  
  98.     }  
  99. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值