一.线程的状态
线程包括几个状态
-
创建(new) Thread thread=new Thread;
-
就绪(runnable) thread.start();
-
运行(running) 线程只能从就绪状态进入到运行状态
-
阻塞(blocked) 该状态是线程因为某种原因放弃了cpu使用权限,暂时停止运行。
阻塞的情况有三种:
1)等待:调用了wait(),线程进入等待阻塞状态。此时线程需要等待某项工作的完成
2)同步:当线程获取synchronized同步锁失败(此时锁被其他线程暂时占用),线程进入同步阻塞状态。此时该线程需要等其他线程使用完锁。
3)其他:当调用了sleel()或join()或发出了I/O请求时,线程进入阻塞状态。此时线程需要等sleep()时间结束或被打断、join()中断、I/O处理完成,线程才能重新进入就绪状态。
- 消亡(dead)
1)线程中run()或call()执行完成,正常结束
2)线程抛出Exception或error,异常退出
3)线程调用stop()结束—容易导致死锁,不建议
注意:同一时刻线程都只有一种状态,通过线程中getState方法可以获取线程的状态。
二.线程创建的三种方式
1)继承Thread
private class MyThread extends Thread {
SyncThread(String name) {
super(name);
}
@Override
public void run() {
//执行耗时操作
}
}
2)实现Runnable接口
private class MyThread implements Runnable {
@Override
public void run() {
//执行耗时操作
}
}
3) 匿名内部类创建线程
new Thread(new Runnable() {
public void run() {
//执行耗时操作
}
}).start();
三.线程间通信
1.Handler机制
//主线程中定义handler
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//ui更新操作
}
}
};
//开启子线程,通过handler.sendMessage()发消息,通知handler完成ui更新
new Thread(){
@Override
public void run() {
super.run();
for (int i=0;i<3;i++){
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Message message=new Message();
message.what=1;
message.obj="消息";
handler.sendMessage(message);
}
}.run();
注意: Handler对象必须定义在主线程中,如果是多个类直接互相调用,就不太方便,需要传递content对象或通过接口调用。
弊端: Handler机制与Activity生命周期不一致的原因,容易导致内存泄漏,不推荐使用。
2.runOnUiThread
new Thread(){
@Override
public void run() {
super.run();
for (int i=0;i<3;i++){
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//ui更新操作
}
});
}
}.run();
用Activity对象的runOnUiThread方法更新,在子线程中通过runOnUiThread()方法更新UI,强烈推荐使用。
3.View.post(Runnable r)
new Thread(){
@Override
public void run() {
super.run();
for (int i=0;i<3;i++){
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
tv.post(new Runnable() {
@Override
public void run() {
//ui更新操作
}
}); }
}.run();
这种方法更简单,但需要传递要更新的View过去,推荐使用
4.AsyncTask
new AAsyncTask().execute("消息");
private class AAsyncTask extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects) {
for (int i=0;i<3;i++){
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return objects[0].toString();
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//ui更新操作
}
}
四.Thread线程同步
1)为什么需要线程同步?
防止多个线程访问一个数据对象时,出现数据不一致的问题。
2)线程同步的几种方式
1)同步函数同步函数
private synchronized void count() {
if (count > 0) {
Log.e(TAG, Thread.currentThread().getName() + "--->" + count--);
} else {
isRunning = false;
}
}
2)同步代码块
private void count() {
synchronized (this) {
if (count > 0) {
Log.e(TAG, Thread.currentThread().getName() + "--->" + count--);
} else {
isRunning = false;
}
}
}
3)使用特殊域变量(volatile)实现线程同步
private volatile int count = 1000;
4)使用重入锁实现线程同步
private void count() {
lock.lock();
if (count > 0) {
Log.e(TAG, Thread.currentThread().getName() + "--->" + count--);
} else {
isRunning = false;
}
lock.unlock();
}
网友评论