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);
}
}