CompletableFutureTest.java 975 B

123456789101112131415161718192021222324252627282930313233343536
  1. package com.xjrsoft.xjrsoftboot;
  2. import org.apache.tools.ant.taskdefs.Sleep;
  3. import org.junit.jupiter.api.Test;
  4. import java.util.concurrent.CompletableFuture;
  5. public class CompletableFutureTest {
  6. @Test
  7. public void CompletableFutureTest() throws InterruptedException {
  8. CompletableFuture.runAsync(() -> {
  9. try {
  10. Thread.sleep(2000); // 暂停2秒
  11. System.out.println(Thread.currentThread());
  12. } catch (InterruptedException e) {
  13. throw new RuntimeException(e);
  14. }
  15. System.out.println(1);
  16. });
  17. CompletableFuture.runAsync(() -> {
  18. try {
  19. Thread.sleep(1000); // 暂停1秒
  20. System.out.println(Thread.currentThread());
  21. } catch (InterruptedException e) {
  22. throw new RuntimeException(e);
  23. }
  24. System.out.println(2);
  25. });
  26. Thread.sleep(1000000);
  27. }
  28. }