AsyncTask设置优先级

时间:2017-06-16 21:14:35

标签: java android multithreading android-asynctask

我有一个AsyncTask(.execute())方法onPostExecute。此方法启动另一个AsyncTask(.execute()),需要在继续执行第一个onPostExecute之前完成。是否可以暂停第一个线程并等待第二个线程完成?我需要第二个postExecute方法的结果才能完成第一个postExecute

以下示例:

public class RetrieveData extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... strings) {
        HttpURLConnection conn = null;
        try {
            URL url = new URL(strings[0]);
            conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");

            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String linieNoua = "";
            String crlf = System.getProperty("line.separator");
            StringBuilder sb = new StringBuilder();
            while((linieNoua = br.readLine()) != null) {
                sb.append(linieNoua);
                sb.append(crlf);
            }
            conn.disconnect();
            return sb.toString();
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

RetrieveData retrieveData = new RetrieveData() {
        @Override
        protected void onPostExecute(String s) {
            if (s != null) {
               retrieveTransport(transportRegNr);
            } else {
                Toast.makeText(getApplicationContext(), R.string.login_server_error, Toast.LENGTH_LONG).show();
            }
        }
    };
    retrieveData.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://192.168.0.101:3000/route/" + prefs.getString("email",null));
}

private void retrieveTransport(String regNr){

    RetrieveData retrieveData = new RetrieveData() {
        @Override
        protected void onPostExecute(String s) {
            if (s != null) {
                try {
                    JSONObject jsonObject = new JSONObject(s);

                    String model = jsonObject.getString("model");
                    String regNr = jsonObject.getString("regNr");
                    int type = jsonObject.getInt("type");
                    int seats = jsonObject.getInt("seats");

                    t = new Transport(model,regNr,null,seats,type);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } else {
                Toast.makeText(getApplicationContext(), R.string.login_server_error, Toast.LENGTH_LONG).show();
            }
        }
    };
    retrieveData.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://192.168.0.101:3000/transport/registryNr/" + regNr);
}

如果我使用execute方法,则永远不会调用来自onPostExecute的{​​{1}}。如果我使用retrieveTransport(String regNr),它们会同时运行,这也不好。我需要完成第一个executeOnExecutor;没有它,我无法继续第一个retrieveTransport

1 个答案:

答案 0 :(得分:0)

使用

  

的getStatus()

检查AsyncTask是挂起,正在运行还是已完成。当finsh启动你的新任务时。似乎:

if(retrieveTransport.getStatus() == AsyncTask.Status.PENDING){
    // My AsyncTask has not started yet
}

if(retrieveTransport.getStatus() == AsyncTask.Status.RUNNING){
    // My AsyncTask is currently doing work in doInBackground()
}

if(retrieveTransport.getStatus() == AsyncTask.Status.FINISHED){
    // START NEW TASK HERE
}

您应用的示例:

        if (retrieveTransport!= null && retrieveTransport.getStatus() == AsyncTask.Status.FINISHED) {
           //START retrieveData TASK HERE
        }
        else
        {
          //IGNORE 
        }