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方法来实现一个线程

 

view plain copy to clipboard print ?
  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. }  

 

启动线程:

 

view plain copy to clipboard print ?
  1. new MyThread().start();  

 

2.创建一个Runnable对象

 

view plain copy to clipboard print ?
  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. }  

 

启动线程:

 

view plain copy to clipboard print ?
  1. // providing a new Thread instance with a Runnable object during its creation.  
  2.         new Thread(new MyRunnable()).start();  

 

3.通过Handler启动线程

 

view plain copy to clipboard print ?
  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. }  

更多相关文章

  1. Android Activity onConfigurationChanged()方法 监听状态改变
  2. 浅析Android线程模型一 --- 转
  3. android中线程的应用
  4. Android进程与线程基本知识
  5. Android高手进阶教程(二十)之---Android与JavaScript方法相互调
  6. android中的坐标系以及获取坐标的方法
  7. Android与JavaScript方法相互调用
  8. 创建Android库的方法及Android .aar文件用法小结

随机推荐

  1. SpringBoot集成tkMybatis插件
  2. docker socket文件打开数量过多分析
  3. html内table表格的与form表单基础写法!
  4. html元素第一课
  5. 3.19作业下拉框
  6. Spring Boot + Vue3 前后端分离实战wiki
  7. 作业20210321
  8. C++中的const
  9. Spring Cloud 2.x之Activiti整合规则引擎
  10. 谷歌重返中国最受伤的是谁?