我在java中的线程有点问题。当我在MainActivity's
onCreate中打开我的应用程序时,我开始在后台下载数据。假设下载将以11:46:43.641结束。
当我想使用另一个线程做某事时,我在11:46:00000开始。在run
方法结束后,我必须等到11:46:43.641开始doInBackground
。所以我必须等待第一个线程完成它的工作。
是否可以同时运行这两个线程?
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);
}
}
这就是我在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);
}
}