The basic concept of the thread pool to achieve (a) thread pool

Begins

This series about the subject, I also tangled for a while. I do not know how to name, originally thought called "Java thread pool code is interpreted," but then I interpret simple code, it may not be able to understand the essence of its design. Thought to want to, think it is "the realization of a thread pool" is more appropriate. why?

Many people may have doubts, since Java has provided excellent thread pool, why do we have to consider to achieve a thread pool, it does not say not to repeat Well-create the wheel. I think so, when a person wants to be excellent "artisan" in the face of a well-designed "wheels", always irrepressible curiosity and urge to know the "wheels" is how come the cause.

Since the type that want to know "wheel" is made of how we look directly at "drawing" you can not be proud, but also why and then draw it again, "drawing" mean?

There is a big gap of , when we look at "drawing", there is always a kind of illusion, I feel it should be so designed, while ignoring the most important issue, why should such a design it . If we are not human-create the wheel, we are not deep understanding. And when we try to "repeat" when-create the wheel, we will and good "artisan" collision of ideas, this idea of a collision, it will have a profound memories and reflection.

Why do you need a thread pool?

Thread pool itself is one of pooling techniques, and database connection pool, the use of the thread pool is mainly due to the following considerations

  • Reduce overhead when creating, saving performance
  • Unified management, to facilitate monitoring

We see an example of a single-threaded, a thread of the complete cycle takes roughly three steps

  1. Thread creation and resource allocation (T1)
  2. Thread execution (T2)
  3. End of the thread and resource recovery (T3)

Length T = T1 + T2 + T3 fact, we can see that, in addition to T2, T1 and T3 can be freed up by the whole execution thread pool threads. This is a very important goal of the thread pool, thread pool managed thread creation and destruction, that is, the thread pool is a thread lifecycle management , which is very important.

A thread pool should have what conditions?

  • Life cycle management of threads, including the creation and recovery
  • The life cycle of the thread pool, initialization, execution, destruction, etc.
  • Mission, referring specifically to each thread of execution
  • Task queue, mainly used for storage tasks, caching wait.

Guess you like

Origin juejin.im/post/5d04e2466fb9a07ea803c88f