I love the java series --- [principle] thread pool

First, the threads are not better?

Made truck (thread) to do the time?-Time use, run out of terrible destruction, destruction or not time-consuming?
➢ 1, a thread is an object in java, java each thread needs an operating system thread support. Thread creation, destruction takes time.
  If you create a time + time to destroy> mission time is very uneconomical.
➢ 2, java heap memory occupied by objects, operating system threads consume system memory, according to jvm specification, the default maximum stack size of a thread 1M, the stack
  space is the need to allocate from the system memory.
  Too many threads, will consume a lot of memory.
◆ made a lot of trucks, may need space to put them, will not cause memory is tight?
➢ 3, the operating system needs frequent thread context switching (everyone wants to be run), affect performance.

Second, how to use multithreading correct? (Thread Pool)

➢ multi-threaded Objective: To make full use of cpu concurrent work (to do more)

➢ the nature of the thread: code execution to the cpu

➢ continue with the appropriate number of trucks transporting code.

➢ appropriate number of threads that constitute a pool.

➢ If there are tasks to be performed, then into the pool, a thread pool of the transport task to perform cpu.

Third, the thread pool works

 

Fourth, the handwriting thread pool steps

1. The task represents what?

 

Runnable: no return value, only responsible for the implementation.

. Callable: 1 has a return value, the return value FutureTask receiving the object, when taken directly get;

      2. and may throw an exception.

2. The warehouse is represented by what?

 

 3. Code Model (to be improved)

 

4. How to determine the appropriate number of threads?

Encounter this problem, do not rush to answer, ask first: the maximum size of your server is how much?

reference:

 

 

 

 

Guess you like

Origin www.cnblogs.com/hujunwei/p/11915093.html
Recommended