我想在线程中每隔2秒向服务器发送请求,并检查是否有东西给我回给我....为了获得价值,我必须使用可调用。我无法弄清楚如何在每2秒运行调用线程,并获得价值从回来...这里是可调用的执行我的示例代码...可调用接口Loop stop主要方法进一步执行
public String call(){
boolean done = true;
String returnData = "";
while(done){
try {
returnData = post.getAvailableChat();
if(!returnData.equals("")){
System.out.println("Value return by server is "+returnData);
return returnData;
}
return null;
} catch (IOException ex) {
done = false;
Logger.getLogger(GetChatThread.class.getName()).log(Level.SEVERE, null, ex);
}
现在,这里是我的主类代码我知道我做错了这里的主类,因为我的代码不会去下一行while循环之后....但请告诉我该怎么办呢
Callable<String> callable = new CallableImpl(2);
ExecutorService executor = new ScheduledThreadPoolExecutor(1);
System.err.println("before future executor");
Future<String> future;
try {
while(chatLoop_veriable){
future = executor.submit(callable);
String serverReply = future.get();
if(serverReply != null){
System.out.println("value returned by the server is "+serverReply);
Thread.sleep(2*1000);
}//End of if
}//End of loop
} catch (Exception e) {
我会尽力而且比标记你的答案有效......谢谢你的帮助...... –