Android 中三种使用线程的方法

Android 中三种使用线程的方法

public class

Thread

extends Object
implements Runnable

There are basically two main ways of having a Thread execute application code. One is providing a new class that extends Thread and overriding its run() method. The other is providing a new Thread instance with aRunnable object during its creation(提供给它的实例化). In both cases, the start() method must be called to actually execute the new Thread.

 

 

Thread()

Constructs a new   Thread  with no   Runnable  object and a newly generated name.

 

Thread(Runnable runnable)

Constructs a new   Thread  with a   Runnable  object and a newly generated name.

 在多线程编程这块,我们经常要使用Handler,Thread和Runnable这三个类,那么他们之间的关系你是否弄清楚了呢?

  首先说明Android的CPU分配的最小单元是线程,Handler一般是在某个线程里创建的,因而Handler和Thread就是相互绑定的,一一对应。

  而Runnable是一个接口,Thread是Runnable的子类。所以说,他俩都算一个进程。

  HandlerThread顾名思义就是可以处理消息循环的线程,他是一个拥有Looper的线程,可以处理消息循环。

  与其说Handler和一个线程绑定,不如说Handler是和Looper一一对应的。

Handler是沟通Activity Thread/runnable的桥梁。而Handler是运行在主UI线程中的,它与子线程可以通过Message对象来传递数据

1.通过继承Thread类,并改写run方法来实现一个线程

 

  1. public class MyThread extends Thread {  
  2.       
  3.     //继承Thread类,并改写其run方法  
  4.       
  5.     private final static String TAG = "My Thread ===> ";  
  6.       
  7.     public void run(){  
  8.         Log.d(TAG, "run");  
  9.         for(int i = 0; i<100; i++)  
  10.         {  
  11.             Log.e(TAG, Thread.currentThread().getName() + "i =  " + i);  
  12.         }  
  13.     }  
  14. }  

 

启动线程:

 

  1. new MyThread().start();  

 

2.创建一个Runnable对象

 

  1. public class MyRunnable implements Runnable{  
  2.     private final static String TAG = "My Runnable ===> ";  
  3.       
  4.     @Override  
  5.     public void run() {  
  6.         // TODO Auto-generated method stub  
  7.         Log.d(TAG, "run");  
  8.         for(int i = 0; i<1000; i++)  
  9.         {  
  10.             Log.e(TAG, Thread.currentThread().getName() + "i =  " + i);  
  11.         }  
  12.     }  
  13. }  

 

启动线程:

 

  1. // providing a new Thread instance with a Runnable object during its creation.  
  2.         new Thread(new MyRunnable()).start();  

 

3.通过Handler启动线程

 

  1. public class MainActivity extends Activity {  
  2.       
  3.     private final static String TAG = "UOfly Android Thread ==>";  
  4.     private int count = 0;  
  5.     private Handler mHandler = new Handler();  
  6.     private Runnable mRunnable = new Runnable() {  
  7.         public void run() {  
  8.             Log.e(TAG, Thread.currentThread().getName() + " " + count);  
  9.             count++;  
  10.             setTitle("" + count);  
  11.             // 每3秒执行一次  
  12.             mHandler.postDelayed(mRunnable, 3000);  //给自己发送消息,自运行
  13.         }  
  14.     };  
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         // 通过Handler启动线程  
  21.         mHandler.post(mRunnable);  //发送消息,启动线程运行
  22.     }  
  23.       
  24.       @Override      
  25.          protected void onDestroy() {       
  26.              //将线程销毁掉       
  27.              mHandler.removeCallbacks(mRunnable);       
  28.              super.onDestroy();       
  29.          }       
  30.       
  31. }  
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值