블러킹(Blocking)

blocking

블러킹은 다른 메서드를 호출할 때 제어권도 다 넘겨주고 작업이 끝난 후에 돌려 받는 것을 말한다.

예제는 2개의 클래스로 진행되며 Data, Main으로 진행 된다.

블러킹 예제

DataSync Class

public class DataSync {
    private int id;
    private long simulationDuration;

    public DataSync(int id, long simulationDuration) {
        this.id = id;
        this.simulationDuration = simulationDuration;
    }

    public String get() {
        try {
            Thread.sleep(this.simulationDuration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "data-" + id;
    }
}

MainSync Class

public class MainSync {
    public static void main(String[] args) {
        long startTime, endTime;

        DataSync dataSync1 = new DataSync(1, 5000); // 5s
        DataSync dataSync2 = new DataSync(2, 2000); // 2s
        DataSync dataSync3 = new DataSync(3, 3000); // 3s

        startTime = System.currentTimeMillis();
        System.out.println("Start");

        String d1 = dataSync1.get();
        printData(d1);

        String d2 = dataSync2.get();
        printData(d2);

        String d3 = dataSync3.get();
        printData(d3);

        endTime = System.currentTimeMillis();
        System.out.println("Done");

        System.out.print("Execution time (ms): " + (endTime - startTime));
    }

    private static void printData(Object data) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Synchronously printing " + data);
    }
}

이 예제의 출력값을 보게되면 순차로 출력이 된걸 볼 수 있다.

Start
Synchronously printing data-1
Synchronously printing data-2
Synchronously printing data-3
Done
Execution time (ms): 13046

이런 방식을 블러킹 방식이라 한다.

넌블러킹(Non-Blocking)

non-blocking

블러킹과 다르게 제어권을 계속 가지고 있기 때문에 작업의 완료여부와 상관없이 다른 작업을 수행할 수 있는것을 말한다.

예제는 2개의 클래스로 진행되며 Data, Main으로 진행 된다.

넌블러킹 예제

DataASync Class

public class DataAsync implements Supplier<String> {
    private int id;
    private long simulationDuration;

    public DataAsync(int id, long simulationDuration) {
        this.id = id;
        this.simulationDuration = simulationDuration;
    }

    @Override
    public String get() {
        try {
            Thread.sleep(this.simulationDuration);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "data-" + id;
    }
}

MainAsync Class

public class MainAsync {
    public static void main(String[] args) {
        long startTime, endTime;

        CountDownLatch latch = new CountDownLatch(3);
        DataAsync dataAsync1 = new DataAsync(1, 5000); // 5s
        DataAsync dataAsync2 = new DataAsync(2, 2000); // 2s
        DataAsync dataAsync3 = new DataAsync(3, 3000); // 3s

        startTime = System.currentTimeMillis();
        System.out.println("Start");

        try {
            CompletableFuture.supplyAsync(dataAsync1).thenAccept(d1 -> {
                printData(d1);
                latch.countDown();
            });

            CompletableFuture.supplyAsync(dataAsync2).thenAccept(d2 -> {
                printData(d2);
                latch.countDown();
            });

            CompletableFuture.supplyAsync(dataAsync3).thenAccept(d3 -> {
                printData(d3);
                latch.countDown();
            });

            latch.await();
        } catch (Exception e) {
            e.printStackTrace();
        }

        endTime = System.currentTimeMillis();
        System.out.println("Done");

        System.out.print("Execution time (ms): " + (endTime - startTime));
    }

    private static void printData(String data) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Synchronously printing " + data);
    }
}

넌블러킹의 실행된 예제의 출력값을 확인하면 위의 블러킹 방식과는 다르게 순차적으로 진행되지 않고 먼저 실행이 끝나는 순으로 출력된 모습을 볼 수 있다.

Start
Synchronously printing data-2
Synchronously printing data-3
Synchronously printing data-1
Done
Execution time (ms): 6015

이런 방식을 넌블러킹이라한다.

참조

- 끗 -

image

500미터