Skip to content

Latest commit

 

History

History
59 lines (53 loc) · 1.27 KB

exam-03-03.adoc

File metadata and controls

59 lines (53 loc) · 1.27 KB

Exam—​3-3. Thread pool을 이용해 RunnableCounter를 실행해 보자.


과정

  1. ExecutorService를 이용해 thread pool을 생성한다. 이때, pool의 크기는 1로 한다.

    ExecutorService pool = Executors.newFixedThreadPool(1);
  2. Thread pool에 RunnableCounter instance를 생성해 넘기고 실행하도록 한다.

    pool.execute(new RunnableCounter("counter1", 5));
    pool.execute(new RunnableCounter("counter2", 5));
  3. Thread pool을 종료하도록 명령을 내리고, 종료되길 기다린다.

    pool.shutdown();
    System.out.println("Shutdown called");
    while (!pool.awaitTermination(2, TimeUnit.SECONDS)) {
        System.out.println("Not yet finished");
    }
    System.out.println("All service finished");
  4. 실행 결과는 아래와 같다.

    counter1 : 1
    counter1 : 2
    counter1 : 3
    counter1 : 4
    counter1 : 5
    counter2 : 1
    counter2 : 2
    counter2 : 3
    counter2 : 4
    counter2 : 5

[돌아가기](../03.runnable_interface_implement.adoc)