Thread pool (Java interview questions)

1. Why does OOM exception occur in CachedThreadPool?

Too many threads were created, memory overflowed

2. If a task is to be added to the thread pool, what is the processing flow?

First determine whether the task of blocking queue is full, full to see if is to achieve a kind of denial strategy, if not full, then added to the task queue blocking, set of threads
together to judge whether the number of tasks is greater than the core number of threads, created greater than For queues, the maximum number of threads does not exceed the maximum. If the number of threads exceeds the idle time, the
number of threads is reduced to the number of core threads.

3. Talk about the rejection strategy of the thread pool

The first type: directly discard the new task
The second type: directly discard the new task and throw an exception The
third type: discard the oldest task and add a new task The
fourth type: directly use the current thread to execute the task

4. What are the core attributes of the thread pool?

Number of core threads, maximum number of threads, thread idle time, blocking queue, rejection strategy, etc.

5. What are the four kinds of thread pools provided by JDK?

The first type: a single thread thread pool (newSingleThreadExecutor), singleton mode, the thread is abnormal and re-create the thread.
The second type: a fixed thread number thread pool (newFixedThreadPool). The
third type: a timed thread pool (newScheduledThreadPool) Can perform delayed tasks, can perform tasks with return values

6. What's wrong with these thread pools provided by JDK?

A fixed number of thread pools and a single thread thread pool may accumulate a large number of tasks, resulting in the OOM
cache thread pool and the timed thread pool may create a large number of threads, resulting in OOM
fourth: cached thread pool (newCachedThreadPool)

7. How to design the thread pool size?

This depends on whether the task type is CPU-intensive or IO-intensive. If it is CPU-intensive, it is set to the number of CPU cores + 1 thread. If it
is IO-intensive, because IO operations do not occupy the CPU, you can set Double the number of CPU + 1 thread.

8. Do you know how to implement the bottom of the thread pool? What data structure is used?

9. If you were to write a thread pool, what would you write and what modules are there?

10. How to create a new thread in your thread pool

11. How to write a simple thread pool?

(First create a thread pool class, which has some core attributes, such as the
maximum number of core threads , task blocking queue, thread collection, and thread idle time) The
first step: warm up, create a core thread through the constructor and start, and There is the creation of a blocking queue LinkedBolockingQueue. The
second step: add a task, create an execute method, use the offer to add the task to the blocking queue. The
third part: consume the task, create a worker thread internal class, and continuously call the poll method through the while loop
to get Task, call the run method of runnable to execute the task.

12. Why use thread pool?

If threads are frequently created and destroyed, CPU consumption is very large, you need to consider the use of thread reuse

13. What are the advantages of using thread pool?

Reuse threads to reduce the CPU consumption of creating and destroying threads. There is also unified management of threads

Published 51 original articles · Likes2 · Visits 1849

Guess you like

Origin blog.csdn.net/qq_42972645/article/details/105658199