Defer

Defer 는 지연 이라는 의미를 갖고 있다.

Lazy 도 지연이라는 의미를 갖고 있긴한데, StackOverflow 에서는 다음과 같이 설명하고 있다.

  • Lazy means "don't do the work until you absolutely have to."
  • Deferred means "don't compute the result until the caller actually uses it."

예를 들어 PaymentService 에 후불 계산이라는 기능이 추가된다면 Deferred 라는 단어를 사용하여 그 의미를 표현할 수 있다.

Reactive Programming

Mono<LocalDateTime> deferMono = Mono.defer(() -> Mono.just(LocalDateTime.now()));
deferMono.subscribe(System.out::println); // 구독하는 시점에 데이터를 emit 하는 Mono 를 생성

Coroutines

Job, Async, Deferred

fun main(): Unit = runBlocking {
    val time = measureTimeMillis {
        val one: Deferred<Int> = async { call1() }
        val two: Deferred<Int> = async { call2() }
        println("The answer is ${one.await() + two.await()}")
    }
    println("Completed in $time ms") // ex. Completed in 1017 ms
}

suspend fun call1(): Int {
    delay(1000L)
    return 1
}

suspend fun call2(): Int {
    delay(1000L)
    return 2
}

Deferred Execution with Lambdas

Cay S. Horstmann 은 Java SE 8 for the Really Impatient: Programming with Lambdas 에서 Lambda 를 사용하는 주된 이유는 Deferred Execution 을 위해서라고 말한다.

The main reason for using a lambda expression is to defer the execution of the code until an appropriate time. The point of all lambdas is deferred execution.

There are many reasons for executing code later, such as:

Deferred Execution with Java's Supplier

Supplier 를 통해 Deferred Execution 을 구현할 수 있다.

다른 장점으로는 Deferring Alternative Calculation for Optional Until Known to Be Necessary 가 있다.

Examples by ChatGPT:

public class DeferredExample {
    public static void main(String[] args) {
        // Create a CompletableFuture to represent the deferred computation
        CompletableFuture<Integer> futureResult = CompletableFuture.supplyAsync(new DeferredComputation());

        // Get the result when it's ready
        futureResult.thenAccept(result -> System.out.println("Result: " + result));
    }
}

class DeferredComputation implements Supplier<Integer> {
    @Override
    public Integer get() {
        // Simulate a time-consuming computation
        try {
            Thread.sleep(3000); // Simulate a 3-second computation
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return 42;  // Return the result of the computation
    }
}

Deferred Execution with Java’s Consumer

Consumer 를 통해서도 Deferred Execution 을 구현할 수 있다.

Examples by ChatGPT:

public class DeferredConsumerExample {
    public static void main(String[] args) {
        // Create a CompletableFuture to represent the deferred computation
        CompletableFuture<Integer> futureResult = CompletableFuture.supplyAsync(() -> {
            // Simulate a time-consuming computation
            try {
                Thread.sleep(3000); // Simulate a 3-second computation
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 42;  // Return the result of the computation
        });

        // Handle the result using a Consumer
        futureResult.thenAcceptAsync(result -> {
            // This is executed when the computation is complete
            System.out.println("Result: " + result);
        });
    }
}

References

  • Java SE8 for the Really Impatient / Cay S. Horstmann 저