There are two ways to implement multithreading in Java:
1. Implement classes that inherit the thread class (override the Run method)
2. Implement Runnable interface (override the Run method)
The relationship between the two methods mentioned above;
Having seen the JDK, you'll find that the thread class implements the Runnable interface.
It can be seen that the implementation of the Runnable interface has the following significant advantages over inheriting the thread class: (1), suitable for multiple threads of the same program code to handle the same resource situation, the virtual CPU (line
The effective separation of code and data from the program, which embodies the object-oriented design concept.
(2), can avoid the limitations of Java's single inheritance characteristics. Development often encounters such a
Situation, that is, when you want to put a subclass of a class that has been inherited into multi-threading, because a class can not have two parent classes at the same time, so can not inherit the thread class way, then can only adopt the way of implementing the Runnable interface.
(3), enhance the robustness of the program, code can be shared by multiple threads, code and data is independent.
When multiple threads execute code from an instance of the same class, they are said to share the same code. Multiple threads can manipulate the same data, regardless of their code. When the share accesses the same object, the same data is shared. When a thread is constructed, the required code and data are passed in through an object as a constructor argument, an instance of a class that implements the Runnable interface.
Any program in Java has a default main thread, and the main thread is the main () method.
There are 5 states for any thread. Create, ready, run, block, terminate.
Note:
1, threads (thread) refers to the process of running a program. The "multithreading" mechanism can run multiple blocks at the same time,
It also solves the problem that the traditional programming language cannot solve because of the higher efficiency of the program running. 2, if you want to activate the thread in the class, you must first prepare the following two items:
(1), this class must be extended from the thread class, making itself a subclass of it. (2), the processing of threads must be written within the run () method.
3. The run () method is a method defined in the thread class, so the program code of the thread is written in the run ()
method, what you do is overwrite the operation.
4. The abstract run () method is declared in the Runnable interface, so it must be in the class that implements the Runnable interface
define the Run () method explicitly.
5, each thread, before its creation and extinction, will be in one of the following five states: Create, Ready,
run, block, terminate.
6, the suspended state of the thread can be caused by the following conditions:
(1) When the thread calls the object's wait (). (2) When the thread itself calls sleep ().
(3) when the thread joins () with another thread. 7, the reasons for the disappearance of frozen factors are:
(1) If the thread is frozen by the wait () method of the calling object, the object's Notify () method is called
can be lifted when freezing.
(2) The thread enters sleep state, but the specified sleep time is up.
8. When the run () method of the thread finishes running, or the thread calls its stop () method, the thread enters the
The dead state.
9. The sleep () method in the thread class can be used to control the sleep state of the thread, depending on the sleep ()
the parameters are determined.
10. To force a thread to run, the join () method is available.
11. The join () method throws a Interruptedexception exception, so the join () method must be written
within the Try-catch block.
12. When multiple thread objects are manipulating the same shared resource, use the Synchronized keyword to
Synchronous Processing
Multithreading in Java