Wk6L2 - Thread Pools & Executor Service Flashcards

1
Q

What are asynchronous tasks, and why are they useful in multithreading?

A

Asynchronous tasks break down problems into smaller ones, allowing them to be solved with multiple threads. This prevents issues like blocking from I/O or network problems, especially in web servers, where handling multiple requests concurrently is crucial.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the issues with unbounded thread creation?

A

Unbounded thread creation can lead to:

  • Thread lifecycle overhead (too much time spent on creating and destroying threads)
  • Resource consumption (e.g., memory and CPU)
  • Stability issues (too many threads can overwhelm the system).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How does a thread pool solve the issues of unbounded thread creation?

A

A thread pool creates a fixed number of worker threads at the start of a program. These threads are reused to handle tasks, reducing the overhead of creating and destroying threads and improving resource management.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of the Executor Service in Java?

A

The Executor Service manages a pool of threads and allows tasks to be submitted via the submit method. It fits the Producer-Consumer design pattern, ensuring tasks are efficiently handled by worker threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What method is used to gracefully shut down an Executor Service, and how does it work?

A

The shutdown method is used to stop accepting new tasks while allowing ongoing tasks to complete. It ensures an orderly shutdown without abruptly terminating threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the awaitTermination method do in Executor Service?

A

awaitTermination blocks until all tasks are completed after a shutdown is initiated. It waits for a specified time and ensures no tasks are left hanging.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the different types of thread pools in Java?

A
  • Single Thread Executor
  • Fixed Thread Pool
  • Cached Thread Pool
  • Scheduled Thread Pool
  • Fork Join Pool (for recursive algorithms)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between newFixedThreadPool and cachedThreadPool?

A

newFixedThreadPool creates a fixed number of threads to handle tasks, whereas cachedThreadPool dynamically creates new threads as needed and reuses idle threads to manage task execution efficiently.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is the Factory Design Pattern used in the Executor class?

A

The Factory Design Pattern is used to create thread pools in the Executor class, ensuring flexibility and abstraction in managing thread creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Why is it important to use the submit method instead of creating threads manually in Executor Service?

A

Using submit allows tasks to be managed efficiently by the thread pool, reducing overhead and ensuring tasks are queued and executed in an organized manner, rather than creating excessive, unmanaged threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly