sync, async, blocking, non-blocking

메모·2026년 6월 16일

java

목록 보기
1/3
  1. sync vs async:

    • sync: 요청이 완료될 때까지 기다린다.
    • async: 요청이 완료될 때까지 기다리지 않고, 다른 작업을 계속할 수 있다.
  2. blocking vs non-blocking

    • blocking: 제어권을 넘긴다.
    • non-blocking: 제어권을 다시 넘겨 받는다.
  3. 조합:

    blockingnon-blocking
    sync결과가 나올 때 까지 기다린다. 다른 작업을 할 수 없다.결과가 나올 때까지 기다리지만, 제어권을 가지고 있어 다른 작업을 할 수 있다.
    async결과가 나올 때까지 다른 작업을 할 수 있지만, 제어권을 넘겨준다.결과가 나올 때까지 기다리지 않고, 다른 작업을 한다.
  4. doWork 메서드 코드:

     private String doWork(){
        try{
            System.out.println("wait 2 seconds");
            Thread.sleep(2000);
            
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        return "Finish doWork";
    }
  1. sync,blocking 코드:
    private void runSyncBlocking(){
        // sync: 결과가 나올 때까지 기다린다.
        // blocking: 제어권을 넘겨준다.

        System.out.println("run Synchronous+Blocking");

        String result = doWork();
        System.out.println("result = " + result);
    }
  1. sync, non-blocking 코드:
    private void runSyncNonBlocking() {
        // sync: 결과가 나올 때까지 기다린다.
        // non-blocking: 제어권을 다시 넘겨 받는다.

        System.out.println("run Synchronous+Non-Blocking");

        long count = 0;
        FutureTask<String> task = new FutureTask<>(()->doWork());
        Thread thread = new Thread(task);

        thread.start();

        while(task.isDone()){ // 작업이 끝날 때까지 기다린다.
            count++; //제어권이 나에게 있다.
        }

        try{
            String result = task.get();
            System.out.println("result = " + result);
            System.out.println("count = " + count); // 7,510,232,369
        }catch (Exception e){
            e.printStackTrace();
        }
    }
  1. async, blocking 코드:
     private void runAsyncBlocking(){
        // async: 결과가 완료될 때까지 기다리지 않고, 다른 작업을 계속할 수 있다.
        // blocking: 제어권을 넘겨준다.

        System.out.println("run Asynchronous+Blocking");

        FutureTask<String> task = new FutureTask<>(()->doWork());
        Thread thread = new Thread(task);

        thread.start(); // 제어권을 다시 넘겨 받는다.

        try{
            String result = task.get(); // 다른 것을 제어권을 넘겨주는 것을 선택함
            System.out.println("result = " + result);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
  1. async, non-blocking 코드:
	private void runAsyncNonBlocking(){
        // async: 결과가 완료될 때까지 기다리지 않고, 다른 작업을 계속할 수 있다.
        // non-blocking: 제어권을 다시 넘겨 받는다.

        System.out.println("run Asynchronous+Non-Blocking");

        Thread thread = new Thread(()->{
            String result = doWork();
            System.out.println("result = " + result);
        });

        thread.start();

        System.out.println("오늘 할일 끝 내일 와서 결과 볼게");

    }
profile
공부한 것 기록용 블로그입니다.

0개의 댓글