欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

主线程中同步等待异步线程的返回

程序员文章站 2024-02-02 16:43:16
...
ExecutorService executor = Executors.newSingleThreadExecutor();
Future future = executor.submit(new Callable<Object>() {

    @Override
    public Object call() throws Exception {
        return 100;
    }
});
Object obj = future.get();
println(obj);


/**
* Waits if necessary for the computation to complete, and then
* retrieves its result.
future.get();