1. What is the purpose of using multiple threads?
Make full use of cpu resources, concurrent to do more things
2. When will the cpu thread
When blocking wait ()
sleep
3. What is the thread
A stream code execution, completion of execution of a set of codes
This set of codes, called a task
What work is 4.cpu
Code execution
Process: Task Load --- ----> ---- thread delivery ----> cpu (code execution)
The thread is not better
(1) Each thread needs to be supported by an operating system, create a thread destruction time + time> mission time, does not pay
(2) take up the thread stack space, the operating system uses system memory, a default maximum thread stack size 1m, need to allocate memory from the system, the thread will cause too much out of memory
(3) OS thread context switch frequently, affect performance
6. how to properly use multiple threads
Multithreading purpose: cpu utilization component concurrent work
The nature of the thread: code execution to the cpu
With an appropriate number of trucks transporting code to continue
This is an appropriate number of threads constitutes the thread pool
When there are tasks to be performed, will he put in the thread pool. A thread pool will be sent to cpu task execution
Principle thread pool
Accept the task, into the warehouse
Taken from the warehouse worker task execution
The warehouse is empty, the thread enters the blocked state
What task represents
runnable ordinary cars, will be responsible for the task to deal with cpu
callable Racing (observer pattern)
What warehouse
blockingqueue blocking queue thread-safe
For access to blocked space-time, put in the queue is full blockage in the queue