线程--->AsyncTask初步认识

引用了:https://blog.csdn.net/zj510/article/details/51493015

目录
线程--->AsyncTask初步认识_第1张图片
aaa.png

一.说明

1.默认多个AsyncTask异步执行
2.回调都执行在主线程中
private static class InternalHandler extends Handler {  
    public InternalHandler() {  
        super(Looper.getMainLooper());  
    }  
  
    @SuppressWarnings({"unchecked", "RawUseOfParameterizedType"})  
    @Override  
    public void handleMessage(Message msg) {  
        AsyncTaskResult result = (AsyncTaskResult) msg.obj;  
        switch (msg.what) {  
            case MESSAGE_POST_RESULT:  
                // There is only one result  
                result.mTask.finish(result.mData[0]);  
                break;  
            case MESSAGE_POST_PROGRESS:  
                result.mTask.onProgressUpdate(result.mData);  
                break;  
        }  
    }  
}  
3.内部由自己实现的线程池与handle构成

二.基本用法(实例:三)

函数 时间
onPreExecute 任务开始前(ui线程)
doInBackground 任务(次线程)
onProgressUpdate 任务中途发出信号(ui线程)
onPostExecute 任务结束后(ui线程)

三.看看默认异步

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new MyTestTask().execute();
        new MyTestTask().execute();
    }


    private class MyTestTask extends AsyncTask{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            System.out.println("我要开始了");
            Toast.makeText(Main.this, "我要开始了", Toast.LENGTH_SHORT).show();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            sleep(5000);
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            System.out.println("我结束了");
            Toast.makeText(Main.this, "我结束了", Toast.LENGTH_SHORT).show();
        }
    }

    private void sleep(int time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
04-26 08:23:47.141 12455-12455/com.example.rtyui.androidteach I/System.out: 我要开始了
04-26 08:23:47.147 12455-12455/com.example.rtyui.androidteach I/System.out: 我要开始了
04-26 08:23:52.154 12455-12455/com.example.rtyui.androidteach I/System.out: 我结束了
04-26 08:23:57.155 12455-12455/com.example.rtyui.androidteach I/System.out: 我结束了
结论:AsyncTask默认为异步执行

四.如何实现同步

方式1:使用AsyncTask的线程池
new MyTestTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new MyTestTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
方式2:自己定义线程池
ExecutorService executorService = Executors.newFixedThreadPool(2);
new MyTestTask().executeOnExecutor(executorService);
new MyTestTask().executeOnExecutor(executorService);
new MyTestTask().executeOnExecutor(executorService);

你可能感兴趣的:(线程--->AsyncTask初步认识)