I. Conceptual basis
Process: A program that is in progress (literal translation).
Thread: A control unit (execution path) in the process that is responsible for program execution
Multiple paths can be executed in a process, called multithreading.
There must be at least one thread in a process.
Multiple threads are opened to run multi-part code at the same time.
Each thread has its own running content. This content can be called a task that the thread will perform.
Multithreading Benefits: Solves the problem of multiple parts running at the same time.
The disadvantage of multithreading: too many threads will lead to the reduction of efficiency, in fact, the implementation of the application is the CPU is doing a fast switch to complete. This switch is random.
When the JVM starts, it starts multiple threads, and at least two threads can parse it out.
1, the thread that executes the main function, the thread's task code is defined in the main function.
2, the thread responsible for garbage collection.
The thread's name thread-number (starting at 0) can be obtained through thread's getname, and the main thread name is main.
The purpose of creating a thread is to open an execution path and run the specified code and other code to run concurrently. The specified code that runs is the task of this execution path. The tasks of the main thread created by the JVM are defined in the main function.
and the custom thread where is its task? The thread class is used to describe threads, which require tasks. So the thread class also describes the task. This task is represented by the Run method in the thread class. That is, the Run method is the function that encapsulates the custom thread that runs the task. Defined in the Run method is the task code that the thread will run.
The thread is turned on in order to run the specified code, so only the thread class is inherited and the Run method is replicated. Define the running code in the Run method.
Second, how to create a thread it?
Create thread mode One: Inherit the Thread class.
Steps:
1, define a class that inherits the thread class.
2, overwrite the Run method in the thread class.
3, create a thread for the subclass object that directly creates the thread.
4, call the Start method to open the thread and invoke the thread's task to execute the Run method.
Create thread mode Two: Implement the Runnable interface.
Steps:
1, defines the class implementation runnable interface.
2, overwrite the Run method in the interface, encapsulating the thread's task code into the Run method.
3, the thread object is created through the thread class and the subclass object of the Runnable interface is passed as a parameter to the constructor of the thread class.
Why? Because thread tasks are encapsulated in the Run method of the Runnable interface subclass object, you must explicitly define the tasks to run when you create the Threads object.
4, call the Thread object's Start method to open the thread.
Benefits of implementing the Runnable interface:
1, the task of the thread is separated from the subclass of the thread and carried out in a separate package. Encapsulates a task into an object based on object-oriented thinking.
2, avoids the limitations of Java single inheritance.
Therefore, the second way to create threads is more common.
Three, the difference between synchronous function and synchronous code block:
The lock of the synchronization function is a fixed this.
The lock of the synchronization code block is any object.
It is recommended that you use synchronous code blocks.
Four, thread safety
Causes of thread safety problems:
1, multiple threads are manipulating the shared data.
2, there are multiple threads for manipulating shared data.
When a thread executes multiple code operations that share data, other threads participate in the operation, causing thread safety problems.
Solve the idea;
is to encapsulate the thread code that has multiple operations sharing data, and when the thread is executing the code, the other threads are not allowed to participate in the operation. It is necessary for the other threads to participate in the operation when the code is executed by the front-end thread. In Java, this problem can be solved by synchronizing blocks of code.
Format of the synchronized code block:
Synchronized (object)
{
Code that needs to be synchronized;
}
Benefits of synchronization: Resolves thread security issues.
The disadvantage of synchronization: the relative reduction of efficiency, because the synchronization of the outside of the thread will judge the synchronization lock.
The premise of synchronization: there must be multiple threads in the synchronization and use the same lock.
A thread cannot be opened two times, and an invalid thread state exception is thrown
A static synchronization function uses a lock that is the byte-code file object that the function belongs to, can be obtained using the GetClass method, or it can be represented by the current class name. class.
Deadlock: One of the common scenarios: nesting of synchronizations.
Wait and sleep difference?
1.wait can specify time or not, sleep must specify a time.
2. When synchronizing, the CPU execution and the lock processing are different.
Wait: Release the execution and release the lock.
Sleep: Releases the execution and does not release the lock.
Java multithreaded programming