移动应用开发的过程中需要处理好主线程和子线程的关系耗时操作应该放在子线程中,不然会阻塞主线程(5S),导致ANR,同时刷新UI的操作需要在主线程中执行。一部是提高应用性能的关键,解决主线程和子线程之间的通信问题是关键。
Android中吃力异步的技术主要有Thread,AsyncTask,Handler&looper,Executors
1.Thread异步
1.1.继承Thread类
public class MyThread extends Thread {
private String name;
public MyThread(String name){
this.name = name;
}
@Override
public void run() {
System.out.println(name);
}
}
public class MainTest {
public static void main(String[] args){
MyThread myThread1 = new MyThread("myThread1");
MyThread myThread2 = new MyThread("myThread2");
MyThread myThread3 = new MyThread("myThread3");
myThread1.start();
myThread2.start();
myThread3.start();
}
}
MyThread继承自Thread类,重写run()方法,在run()方法中写上子线程需要执行的任务就可以了。然后在测试的main()方法中new MyThread()。再调用start()方法。这样就完成了使用继承Thread,来实现子线程从创建到执行的整个过程。
1.2实现Runnable接口
public class MyThreadRunnable implements Runnable {
private String name;
public MyThreadRunnable(String name){
this.name = name;
}
@Override
public void run() {
System.out.println(name);
}
}
public class MainTest {
public static void main(String[] args){
MyThreadRunnable r1 = new MyThreadRunnable("r1");
MyThreadRunnable r2 = new MyThreadRunnable("r1");
MyThreadRunnable r3 = new MyThreadRunnable("r1");
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
Thread t3 = new Thread(r3);
t1.start();
t2.start();
t3.start();
}
}
2.AsyncTask
MyAsyncTask基础自AsyncTask,三个泛型分别为,String:是传入参数类型,可以传多个,Integer:是进度显示的参数类型,也可以传多个,Bitmap:是任务执行完的返回类型,这里就是返回一个Bitmap。
使用AsyncTask来实现异步的有点就是简单便捷,各个过程都有明确的回调,过程可控。但是缺点就是要是执行多个异步,就会变得很复杂。
public class MainActivity extends Activity {
private ProgressBar mProgressBar;
private ImageView mImageView;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute("www.baidu.com/xxx.jpg");
}
class MyAsyncTask extends AsyncTask<String, Integer, Bitmap>{
@Override
protected void onPreExecute() {
//这里是开始线程之前执行的,是在UI线程
mProgressBar.setMax(100);
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params) {
//这是在后台子线程中执行的
Bitmap bitmap = null;
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
publishProgress(70);//这里是更新进度
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onCancelled() {
//当任务被取消时回调
super.onCancelled();
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//更新进度
mProgressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
//当任务执行完成是调用,在UI线程
mImageView.setImageBitmap(bitmap);
}
}
}
3.Handler异步
这种一般是配合轮询器进行处理的
public class HandlerTestActivity extends Activity {
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
if(what == 0){
//在主线程中需要执行的操作,一般是UI操作
}
}
};
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
//在子线程中执行任务,执行完成或需要通知UI线程时调用以下方法
mHandler.sendEmptyMessage(0);
}
});
thread.start();
}
}
上面是Handler的一个例子子线程发消息给主线程
public class LooperThreadActivity extends Activity {
private Handler mHandler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new MyThread().start();
//发消息到目标子线程
mHandler.obtainMessage(0).sendToTarget();
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
//1.建立消息循环,初始化Looper
Looper.prepare();
mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
if(what == 0){
//
}
}
};
//启动消息循环
Looper.loop();
}
}
}
上面是主线程发消息给子线程