在java中怎样实现谁先执行完毕谁就输出

使用java8中的CompletableFuture方法进行任务的同步执行


@SpringBootTest
public class CompletableFutureTest {

    @Test
    public void test() {
        // 面试题: 多线程去加载图片, 哪张图片最快获取到, 就直接使用那个, 其他的就不要了
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("正在加载图片1......");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "图片1";
        });

        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("正在加载图片2......");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "图片2";
        });

        CompletableFuture<Object> future = CompletableFuture.anyOf(future1, future2);
        try {
            Object o = future.get();
            System.out.println("最终结果: " + o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java,前端)