在executor(executorservice接口)返回值出现问题几天后,我终于实现了从网络请求返回值。问题是我没有找到准确或类似的wat来返回executor中的值,所以我想知道这是一个粗暴的方式,或者可能有一些缺点,我应该考虑。这是代码示例:
private void someJimming() {
ExecutorService executorService = Executors.newSingleThreadExecutor();
Callable<String> callableString = getMeThis();
Future<String> future = executorService.submit(callableString);
executorService.execute(new Runnable() {
@Override
public void run() {
try {
Log.i("RESULTLongWaited: ", future.get());
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
executorService.shutdown();
while (!future.isDone()) {}
System.out.println("Work done ...");
}
private Callable<String> getMeThis() {
System.out.println("Starting ...");
return () -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "This the Result";
};
}
谢谢你的帮助。。。
暂无答案!
目前还没有任何答案,快来回答吧!