...有没有像 thread.join() 这样的 Callable 方法?
如果线程在池中可用,该pool.submit(callable)方法将返回 aFuture并立即开始执行。要执行 a join,您可以调用future.get()which joins 与线程,返回方法返回的值call()。需要注意的是,如果方法抛出,get()可能会抛出一个。ExecutionExceptioncall()
您不需要将您的包装Callable在FutureTask. 线程池为您做到这一点。所以你的代码是:
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的任何类型。