0
我想發送reqeuest到服務器...直到請求沒有響應我想執行循環打印整數..問題是我的循環繼續下去,如果我執行它直到100它打印100值,如果直到1000它打印1000.我想要的是循環應該依賴於響應。一旦發生反應循環線應採空...FutureTask如何管理線程..?
public class ServiceInteraction {
FutureTask<JSONArray> task;
public JSONArray addUser(List<BasicNameValuePair> postPairs_interaction){
JSONArray jArray;
task = new FutureTask<JSONArray>(new Service_C(postPairs_interaction));
ExecutorService pool = Executors.newFixedThreadPool(2);
pool.submit(task);
for(int i = 0 ; i < 1000; i++){
System.out.println("value is "+i);
}//End of for loop
try{
return (jArray = task.get());
}catch(InterruptedException e){
return null;
}catch(ExecutionException e){
return null;
}
}//End Of addUser
這裏是我SERVICE_C類代碼...
public Service_C(List<BasicNameValuePair> postPairs) {
this.postPairs = postPairs;
}
@Override
public JSONArray call() throws Exception {
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.0.0.62:8080/IDocWS/"+postPairs.get(0).getValue());
post.setEntity(new UrlEncodedFormEntity(postPairs));
ResponseHandler respHandler = new BasicResponseHandler();
String responseData = (String) httpclient.execute(post,respHandler);
try {
JSONArray jArray;
jArray = new JSONArray(responseData);
return jArray;
} catch (JSONException ex) {
Logger.getLogger(Service.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("Error in getting response from the user");
return null;
}
}//End of call method