CompletableFuture 异步处理

CompletableFuture 异步处理

使用于需要同时查询数据的场景

    CompletableFuture<Object> uCompletableFuture1 =
        CompletableFuture.supplyAsync(
            () -> {
              try {
				// 业务代码
				sout("异步测试1")
              } catch (Exception e) {
                e.printStackTrace();
              }
              return true;
            });

    CompletableFuture<Object> uCompletableFuture2 =
        CompletableFuture.supplyAsync(
            () -> {
              try {
				// 业务代码
				sout("异步测试2")
              } catch (Exception e) {
                e.printStackTrace();
              }
              return true;
            });

    CompletableFuture<Object> uCompletableFuture3 =
        CompletableFuture.supplyAsync(
            () -> {
              try {
				// 业务代码
				sout("异步测试3")
              } catch (Exception e) {
                e.printStackTrace();
              }
              return true;
            });

//异步操作结束后,等待全部结果返回后继续操作
	CompletableFuture.allOf(uCompletableFuture1, uCompletableFuture2, uCompletableFuture3).join();

你可能感兴趣的:(java)