Android 中三种使用线程的方法
2016-09-13 11:45
525 查看
Android 中三种使用线程的方法
public classThread
extends Objectimplements Runnable
There are basically two main ways of having a
Threadexecute application code. One is providing a new class that extends
Threadand
overriding its
run()method. The other is providing a new
Threadinstance
with a
Runnableobject during its creation(提供给它的实例化). In both cases, the
start()method
must be called to actually execute the new
Thread.
Thread()
Constructs a new
Threadwith no
Runnableobject
and a newly generated name.
Thread(Runnable runnable)
Constructs a new
Threadwith a
Runnableobject
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 三种线程使用方法
- 使用线程创建的三种方法实现j2me联网(PS:我想做android!NND!)
- Android 中三种使用线程的方法
- Android 中三种使用线程的方法
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Code Fragment-Android在其他线程可以使用方法在UI线程执行UI相关
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android 非UI线程使用View.post()方法一处潜在的内存泄漏
- Android中线程的使用方法
- Android中Handler的使用方法——在子线程中更新界面
- Android中Handler的使用方法——在子线程中更新界面
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android中Handler的使用方法——在子线程中更新界面
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android的消息机制,用Android线程间通信的Message机制,Android中Handler的使用方法——在子线程中更新界面,handler机制
- Android 非UI线程使用View.post()方法一处潜在的内存泄漏