--------------------------------------------------------------------------------------------------
The life cycle of a thread:
New, ready, execute, block, perish
With the new keyword, the thread is created to enter the new State;
When a thread calls the start () method, the thread enters a ready state, meaning that the thread has the right to acquire the time slice of the cup and wait for the JVM to dispatch;
When the thread acquires the cup, the thread is executed, which means that the thread's run () method is executed;
When the thread's run () method does not complete, but loses the cup time slice, at this time the thread enters the blocking state, waits for the next time to be dispatched;
When the thread's run () method executes the settlement, the thread dies;
Multithreading does not improve the execution efficiency of the program, but increases the use of the program by the Cup load probability, multithreading is not parallel execution;
--------------------------------------------------------------------------------------------------
Multithreading: Refers to the program (a process) when the runtime produces more than one thread
Parallel and Concurrency:
Parallel: Multiple CPU instances or multiple machines executing a processing logic at the same time are true at the same time.
Concurrency: Through the CPU scheduling algorithm, let the user appear to execute simultaneously, actually from the CPU operation level is not real at the same time. Concurrency tends to be common in the scene
Resources, the bottleneck is often created for this common resource, and we use TPS or QPS to respond to the system's processing power.
--------------------------------------------------------------------------------------------------
Scheduling and control of threads:
The scheduling model of the thread is divided into: time-sharing scheduling model and preemptive scheduling model, Java using preemptive scheduling model
-
Time-sharing scheduling model: All threads take turns using the CPU's right of use, evenly allocating each thread to occupy the CPU of the chip
Preemptive scheduling model: Priority for high-priority threads to use the CPU, if the priority of the thread is the same, then randomly select a high-priority thread gets more CPU time slices.
--------------------------------------------------------------------------------------------------
How to avoid deadlocks:
Package the same resources, potentially generating deadlocks, into a group and put them in a public lock!
--------------------------------------------------------------------------------------------------
Some basic knowledge of Java threading