Multithreading concurrent programming is an important piece of Java programming, is also the focus of the interview area, or it is worth in-depth study
Concept:
1 Threads: Execution units in a process responsible for program execution
The thread itself relies on the program to run
Threads are sequential control flows in a program and can only use resources and environments that are assigned to programs
2 process: Executing the program
A process contains at least one thread
3 Single threaded: There is only one thread in the program, in fact the main method is a main path
4 Multi-Threading: Run multiple tasks in one program
The goal is to better use CPU resources
Implementation of Threads
There are 2 ways to implement Threads:
Inheriting the thread class and implementing the Runnable Interface (recommended)
Inherit thread
Public class extends Thread { publicvoid run () { System.out.println ("Thread Execution"); } Public Static void Main (string[] args) { new volatiletest (). Start ();} }
Implement Runnable
Public class Consumerworker implements Runnable { publicvoid run () { // Execute logic } }
By invoking the start () method to start the thread, note: Calling the run () method can also execute code, but it does not have another thread but executes the logic in the original thread (the start () method can be understood as creating a thread before the thread executes the run () method)
The state of the thread
- Create (new) state: A Multithreaded object is prepared
- Ready (runnable) state: Called
start()
method, waits for CPU to dispatch
- Run (running) state: Execution
run()
method
- Blocking (blocked) state: Temporarily stops execution and may give resources to other threads for use
- Terminating (dead) state: Thread Destruction
Method of state switching
Start: Starts a thread, and when the Start method is called, the system will open a new thread to execute the user-defined subtask, in which the corresponding thread is assigned the required resources; the creation State becomes ready state
Run: Does not require the user to call, when starting a thread through the Start method, when the thread obtains the CPU execution time, then enters the Run method body to carry on the concrete task; from the ready state to the running state
Wait (): Allows the current "executing thread" to hibernate (pause execution) in the specified number of milliseconds, and the lock is released by the running state into a blocking state
Sleep (): Allows the current "executing thread" to hibernate (suspend execution) in the specified number of milliseconds, from the running state into a blocking state without releasing the lock
Yield: Let the current thread hand over the CPU permissions and let the CPU perform other threads. It is similar to the sleep method and does not release the lock. However, yield does not control the specific time to hand over the CPU, and the yield method only allows threads with the same priority to have the opportunity to get CPU execution time. Changed from run state to ready state
Join: The main thread waits for the child thread to finish before it finishes executing
Interrupt: Pausing a thread
Priority of the thread
In the operating system, threads can prioritize, and higher-priority threads get more CPU resources, which is the task in which the CPU prioritizes higher-priority thread objects.
====== to Be Continued
Java Review notes Multi-threading concurrency