class A{
final B;
B.compute(); // Does executing this line create a new thread since this returns
// a future and the entire function is executed by the same thread ?
}
class B{
Future<Void> compute(){
C obj = factoryOfC.getC();
ListenableFuture<Void> future = executorService.submit (new Callable(){
doSomething();
});
Futures.addCallback(future, new FutureCallback<Void>(){
@Override
void onSuccess(){
}
@Override
void onFailure(){
}
} // end of callback.
} // end of class B
如何在这里定义一个工作单元?哪个线程将在做哪个工作单元? 可调用和回调会在同一个线程中执行吗?Java/Guava中的异步编程
将C类型的obj由相同的线程执行?
谢谢。这有帮助。这个问题呢? C类型的obj是由同一个线程执行的吗? – Phoenix
你的代码中没有迹象表明'obj'是一个Runnable或Callable。它被构建,但没有其他任何事情可以做到。 – slim
它也很大程度上取决于你的ExecutorService实现...... –