import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class FetureTest {
public static void main(String[] args) {
ExecutorService threadPool = Executors.newFixedThreadPool(3);
Future<Integer> future = threadPool.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return new Random().nextInt(500);
}
});
try {
// 获取结果
future.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
多线程 : 使用 Future 获取线程返回结果
最新推荐文章于 2024-07-12 13:58:45 发布