Concept: A child task that can be understood as running independently in a process. For example, QQ.exe is running with a lot of subtasks running, buddy video threads, downloading file threads, transferring data threads, and so on.
Advantages: 1, improve CPU resource utilization, improve system performance.
First, the implementation of multithreading 1.1, inherits the thread class (extends thread), the thread class implements the Runnable interface
Implements Runnable
Extends thread{ @Override void run () {System.out.println ("MyThread");}}
1.2. Implement Runnable interface (implements Runnable)
Implements runnable{ @Override void run () {System.out.println ("MyThread");}}
Note: The use of inherited thread classes to develop multithreaded application design has limitations, because Java does not support multiple inheritance, in order to change this limitation, you can use the implementation, implementation of the Runnable interface (implements Runnable) way to achieve multithreading.
1.3. The randomness of Multithreading: When multi-threading is used, the result of running the code is independent of the order in which the code is executed or called.
Verify:
View Code
Results:
1.4. Non-thread-safe: When multiple threads operate on the same variable in the same object, the value is changed, the value is not synchronized, and the program runs.Non-thread safe
Resolve non-threading security issues by using the Resolve synchronized keyword
View Code
CurrentThread (): Returns which thread the code snippet is being called from. IsAlive (): Determines whether the current thread is active. Sleep (): Specifies the number of milliseconds for the thread that is executing to hibernate. GetId (): Gets the unique identity of the thread two, the thread that cannot be stopped
1. Determine if the thread is a stopped state
In the Java SDK, the Thread.java class provides two methods
This.interrupted (): Tests if the front thread has been interrupted and has been executed with the ability to clear the status ID to False.
This.isinterrupted (); Tests whether the thread is interrupted, but does not identify the purge state.
View Code
One, Java multithreaded Programming core technology (note)--Multithreading implementation