Callable

Runnable๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ Callable์€ ๋ฐ˜ํ™˜๊ฐ’์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค.

Callable

public interface Callable<V> {
    V call() throws Exception;
}

Callable์€ ์„ ์–ธํ•œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ด ์ค๋‹ˆ๋‹ค. Thead์—๋Š” FutureTask์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ณ , Executor์—๋Š” ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค

Drawing

Thread์ด ์‹œ์ž‘๋˜๋ฉด Callable์— ์ž‘์—… ์ „๋‹ฌ์„ ํ•˜๊ณ  ์‘๋‹ต ๊ฐ’์€ future๋‚˜ futrueTask์— ์ „๋‹ฌ์ด ๋ฉ๋‹ˆ๋‹ค.

Main Thread์—์„œ ๊ฐ’์„ ๊บผ๋‚ผ๋•Œ๋Š” Callable์—์„œ ๋ฐ›์•„์˜จ Future&FutureTast์˜ ์‘๋‹ต๊ฐ’์„ ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

Thead

public class CallableWithThead implements Callable<String> {
    @Override
    public String call() throws Exception {
        return "I'm Callable";
    }
}

public class Main {
    public static void main(String[] args) {
        FutureTask<String> futureTask = new FutureTask<String>(new CallableWithThead());
        Thread thread = new Thread(futureTask);
        String result = futureTask.get();
    }
}

Executor

public class CallableWithExecutor implements Callable<String> {
    @Override
    public String call() throws Exception {
        return "I'm Callable";
    }
}

public class Main {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<String> future = executor.submit(new CallableWithExecutor());
        String result = future.get();
    }
}

Improve Future

ํ–ฅ์ƒ๋œ Future์—” CompletableFuture ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

runAsnyc : return X supplyAsync : return 0 ๋‘๊ฐ€์ง€๋กœ ์ž‘์—…์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

public class CallbackSupplier implements Supplier<String> {
    @Override
    public String get() {
        return "I'm Supplier";
    }
}
public class CallbackRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("I'm Runnable");
    }
}

public class Main {
    public static void main(String[] args) {
        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(new CallbackRunnable());
        CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(new CallbackSupplier());

        Void noResult = runAsync.get();
        String result = supplyAsync.get();
    }
}

์ถ”๊ฐ€์ ์œผ๋กœ CompleteFuture์—๋Š” thenApply, thenAccept, thenRun์„ ์‚ฌ์šฉํ•˜์—ฌ์ž‘์—…์„ ์—ฐ๊ฒฐํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

CompletableFuture<Void> runAsync = CompletableFuture.runAsync(new CallbackRunnable())
        .thenRunAsync(new CallbackRunnable());
CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(new CallbackSupplier())
        .thenApply(s -> s + " and thenApply")
        .completeAsync(() -> "I'm completeAsync")
        .exceptionallyAsync(throwable -> "I'm exceptionallyAsync");

Void noResult = runAsync.get();
String result = supplyAsync.get();

thenCompose, thenCombine ์œผ๋กœ ์ž‘์—…์„ ์กฐํ•ฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

CompletableFuture<String> supplyAsync = CompletableFuture.supplyAsync(new CallbackSupplier())
  .thenCombineAsync(CompletableFuture.supplyAsync(new CallbackSupplier()), (s1, s2) -> s1 + s2)
  .thenComposeAsync(s -> CompletableFuture.supplyAsync(() -> s + " and thenComposeAsync"));

์ง€๊ธˆ ๊นŒ์ง€์™€ ๋™์ผํ•œ Printer๋ฅผ ์˜ˆ์ œ๋กœ ์‚ฌ์šฉ

How do code?

Last updated