CH08 - Asynchronous Tasks

  • Thread pool: 기존에 쓰레드를 재사용하자는 취지

  • 자바의 Executor service

image
  • Executors 클래스에서 자주 사용되는 메소드

    • newSingleThreadExecutor() - 싱글

    • newFixedThreadExecutor(int nThreads) - 멀티

1 Thread Pool

image

샐러드를 만든다고 가정.

다음 코드를 쓰레드풀을 만들어서 처리해보자.

결과

쓰레드풀 사용 코드

결과

2 Future

  • Callable 인터페이스

  • 비동기 처리

3 Divide and Conquer

  • RecursiveTask<> 클래스

  • 자바의 Fork Join 프레임워크

    • 효율적인 병렬처리를 위해 자바 7에서 소개된 새로운 기능

    • 가르고 - 합친다

    • fork 순간 다른 쓰레드에서 처리하게 한다.

QUIZ

  • When implementing a recursive divide-and-conquer algorithm in Java, why should you use a ForkJoinPool instead of simply creating new threads to handle each subproblem?

    • The ForkJoinPool manages a thread pool to execute its ForkJoinTasks, which reduces the overhead of thread creation.

  • What does a divide-and-conquer algorithm do when it reaches the base case?

    • Stop subdividing the current problem and solve it.

  • What is the difference between Java's Callable and Runnable interfaces?

    • The Callable interface's call() method returns a result object but the Runnable interface's run() method does not.

  • What is the purpose of a future?

    • It serves as a placeholder to access a result that may not been computed yet.

  • When using a thread pool in Java, the _____ assigns submitted tasks to specific threads within the available pool to execute.

    • thread pool executor service

  • Why are thread pools useful?

    • They reuse threads to reduce the overhead that would be required to create a new, separate thread for every concurrent task.

  • What does a work-to-span ratio less than one indicate?

    • The work-to-span ratio cannot be less than one.

  • What is a program's "span"?

    • sum of the time for all task nodes along the critical path

    • image image

  • What is a program's "critical path"?

    • longest series of sequential operations through the program

  • What is a program's "work"?

    • sum of the time for all task nodes in a computational graph

  • Why are computational graphs useful?

    • They help to identify opportunities for parallel execution.

Last updated