------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to communicating with you. -------
Sometimes multiple actions need to be performed at the same time, and multithreading is used.
Concurrent programs divide programs into separate tasks that are separate from one other.
A thread is a single sequential control flow in the process.
A single process can have multiple tasks that execute concurrently.
You can define threads in two ways:
1. Inherit the thread class:
1. Define class inheritance Thread
2. The run () method of the replication thread
3. Start () of the calling thread
Class Demo Extendsthread {
public void Run () {
}
}
2. Implement Runnable interface
1. Define class implementation Runnable interface
2. Overwrite the Run () method in the Runnable interface
3. Create an object from the thread class
4. Pass the sub-object of the Runnable interface as the actual parameter to the constructor of the thread class
5. Call the Start () method of the thread class to open the thread and invoke the Run method in the Runnable interface subclass
Class Demoimplements Runnable {
public void Run () {
}
}
The second approach is good because it avoids the limitations of single inheritance, and it is recommended to use implementations.
The lock of a synchronized code block is any object
The lock of the synchronization function is the this pointer
The lock of a static synchronization function is a class object
By looking at the API documentation, you can know the various methods in the Runnable interface and thread, and use it in practice.