您的位置:首页 > 移动开发 > Android开发

Android 中三种使用线程的方法

2016-09-13 11:45 525 查看

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 a
Runnable
 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
plaincopy
to clipboardprint?

public class MyThread extends Thread {  

      

    //继承Thread类,并改写其run方法  

      

    private final static String TAG = "My Thread ===> ";  

      

    public void run(){  

        Log.d(TAG, "run");  

        for(int i = 0; i<100; i++)  

        {  

            Log.e(TAG, Thread.currentThread().getName() + "i =  " + i);  

        }  

    }  

}  

 

启动线程:

 

view
plaincopy
to clipboardprint?

new MyThread().start();  

 

2.创建一个Runnable对象

 

view
plaincopy
to clipboardprint?

public class MyRunnable implements Runnable{  

    private final static String TAG = "My Runnable ===> ";  

      

    @Override  

    public void run() {  

        // TODO Auto-generated method stub  

        Log.d(TAG, "run");  

        for(int i = 0; i<1000; i++)  

        {  

            Log.e(TAG, Thread.currentThread().getName() + "i =  " + i);  

        }  

    }  

}  

 

启动线程:

 

view
plaincopy
to clipboardprint?

// providing a new Thread instance with a Runnable object during its creation.  

        new Thread(new MyRunnable()).start();  

 

3.通过Handler启动线程

 

view
plaincopy
to clipboardprint?

public class MainActivity extends Activity {  

      

    private final static String TAG = "UOfly Android Thread ==>";  

    private int count = 0;  

    private Handler mHandler = new Handler();  

    private Runnable mRunnable = new Runnable() {  

        public void run() {  

            Log.e(TAG, Thread.currentThread().getName() + " " + count);  

            count++;  

            setTitle("" + count);  

            // 每3秒执行一次  

            mHandler.postDelayed(mRunnable, 3000);  //给自己发送消息,自运行

        }  

    };  

    /** Called when the activity is first created. */  

    @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

        // 通过Handler启动线程  

        mHandler.post(mRunnable);  //发送消息,启动线程运行

    }  

      

      @Override      

         protected void onDestroy() {       

             //将线程销毁掉       

             mHandler.removeCallbacks(mRunnable);       

             super.onDestroy();       

         }       

      

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android ui 动画 界面 线程
相关文章推荐