0

I have a little problem with threads in java. When I open my app in the MainActivity's onCreate I start a thread to download data in background. Let's say that the download will end at 11:46:43.641.

When I'd like to use another thread to do something so I started it at 11:46:00.000. After the end of the run method I have to wait to start doInBackground for 11:46:43.641. So I have to wait the first thread to finish it's job.

Is it possible to run simultaneously this two thread?

MainActivity.java

public DownloadDataThread downloadDataThread;

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

    downloadDataThread = new DownloadDataThread(this);

    downloadDataThread.run(); 
 }

DownloadDataThread.java

public class DownloadDataThread extends Thread{

Context context;

public DownloadDataThread(Context context){
    this.context = context;
};

public DownloadDataThread(){};

public void run(){

    NetworkUtils utils = new NetworkUtils(context);

    if(!utils.isConnectingToInternet()){
    }else if(utils.isConnectingToInternet()){
        new DataFetcherTask().execute();
    }
}

public static void main(String args[]){
    DownloadDataThread obj=new DownloadDataThread();
    obj.start();
}
public void interrupt(){
    Thread.interrupted();
}

class DataFetcherTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        String serverData = null;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("...");
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            serverData = EntityUtils.toString(httpEntity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            list = new ArrayList<Game>();
            //JSONObject jsonObject = new JSONObject(serverData);
            JSONArray jsonArray = new JSONArray(serverData);

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObjectCity = jsonArray.getJSONObject(i);

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

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}

And this is how I started the other thread somewhere in a fragment:

DownloadGameDetails downloadGameDetails = new DownloadGameDetails(getActivity(),id);
        downloadGameDetails.run();

DownloadGameDetails.java

public class DownloadGameDetails extends Thread{
public Context context;
private String id;

public DownloadGameDetails(Context context,String id){
    this.context = context;
    this.id = id;
};

public DownloadGameDetails(){};

public void run(){
    NetworkUtils utils = new NetworkUtils(context);

    if(!utils.isConnectingToInternet()){
    }else if(utils.isConnectingToInternet()){
        new DataFetcherTask().execute();
    }
}

public static void main(String args[]){
    DownloadGameDetails obj = new DownloadGameDetails();
    obj.start();
}
public void interrupt(){
    Thread.interrupted();
}

class DataFetcherTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        String serverData = null;
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(".....);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            serverData = EntityUtils.toString(httpEntity);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            JSONObject jsonObject = new JSONObject(serverData);

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

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}
T.dog
  • 91
  • 1
  • 12
  • read this http://stackoverflow.com/questions/8579657/whats-the-difference-between-thread-start-and-runnable-run run vs start. Also i see a main method. – Raghunandan Feb 17 '17 at 11:05
  • Thank you for your answer. "Also i see a main method" what do you mean? – T.dog Feb 17 '17 at 11:16
  • We do not have main method as in java in android. – Raghunandan Feb 17 '17 at 11:17
  • Thanks. If I'm using `start` instead of `run` the same thing happen. – T.dog Feb 17 '17 at 11:25
  • I have created two different `Thread` obejct and use `start` method to start them. And the same thing happens. The second thread waits for the first to finish. – T.dog Feb 17 '17 at 11:46
  • http://stackoverflow.com/questions/4068984/running-multiple-asynctasks-at-the-same-time-not-possible Answer is here. – T.dog Feb 19 '17 at 14:28

0 Answers0