java8 : CompletableFuture

This commit is contained in:
haerong22
2021-02-16 15:48:29 +09:00
parent 8a5b47c436
commit eb69ecaba0
2 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
package completableFuture;
public class App {
public static void main(String[] args) throws InterruptedException {
/**
* Thread 사용
*/
// join
Thread thread = new Thread(() -> {
System.out.println("Thread: " + Thread.currentThread().getName());
try {
Thread.sleep(3000L);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
});
thread.start();
System.out.println("Hello: " + Thread.currentThread().getName());
thread.join();
System.out.println(thread + " is finished");
// // interrupt
// Thread thread = new Thread(() -> {
// while (true) {
// System.out.println("Thread: " + Thread.currentThread().getName());
// try {
// Thread.sleep(1000L);
// } catch (InterruptedException e) {
// System.out.println("exit!");
// return;
// }
// }
// });
// thread.start();
// System.out.println("Hello: " + Thread.currentThread().getName());
// Thread.sleep(3000L);
// thread.interrupt();
// // sleep
// Thread thread = new Thread(() -> {
// try {
// Thread.sleep(1000L);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println("Thread: " + Thread.currentThread().getName());
// });
// thread.start();
// System.out.println("Hello: " + Thread.currentThread().getName());
// // 람다 사용
// Thread thread = new Thread(() -> {
// System.out.println("Thread: " + Thread.currentThread().getName());
// });
// thread.start();
// System.out.println("Hello: " + Thread.currentThread().getName());
// // 익명 클래스 사용
// Thread thread = new Thread(new Runnable() {
// @Override
// public void run() {
// System.out.println("Thread: " + Thread.currentThread().getName());
// }
// });
// thread.start();
// System.out.println("Hello: " + Thread.currentThread().getName());
// // Thread 상속해서 사용
// MyThread myThread = new MyThread();
// myThread.start();
//
// System.out.println("Hello: " + Thread.currentThread().getName());
}
}

View File

@@ -0,0 +1,9 @@
package completableFuture;
public class MyThread extends Thread{
@Override
public void run() {
System.out.println("Thread: " + Thread.currentThread().getName());
}
}