... Callable の thread.join() のようなメソッドはありますか?
このpool.submit(callable)メソッドは を返しFuture、スレッドがプールで使用可能な場合はすぐに実行を開始します。を実行するには、メソッドによって返された値を返す、スレッドとの結合をjoin呼び出すことができます。メソッドがスローした場合、がスローされる可能性があることに注意することが重要です。future.get()call()get()ExecutionExceptioncall()
でラップする必要はありません。スレッドプールがそれを行います。したがって、コードは次のようになります。CallableFutureTask
pool = Executors.newSingleThreadExecutor();
Future<String> future = pool.submit(new MyCallable(name, type));
// now you can do something in the foreground as your callable runs in the back
// when you are ready to get the background task's result you call get()
// get() waits for the callable to return with the value from call
// it also may throw an exception if the call() method threw
String value = future.get();
もちろん、これはあなたのMyCallable道具の場合です。Callable<String>はFuture<?>、あなたがどんなタイプであっても一致しますCallable。