is an entity in the process. It is the basic unit that is independently scheduled and dispatched by the system. The thread itself does not own system resources, only some resources that are essential during operation, but it can belong to the same process as Other threads share all resources owned by the process. A thread can create and destroy another thread, and multiple threads in the same process can execute concurrently. Due to the mutual constraints between threads, threads show discontinuity in their operation. Threads also have three basic states: ready, blocked and running.
A thread is a single sequential control process in a program. Running multiple threads at the same time in a single program to complete different tasks is called multi-threading.
The difference between a thread and a process is that the child process and the parent process are different The code and data space, while multiple threads share the data space, each thread has its own execution stack and program counter for its execution context. Multi-threading is mainly to save CPU time and make full use of it, depending on the specific situation. Threads The computer's memory resources and CPU need to be used during operation
Thread cycle
New ready operation blocked and died
Thread scheduling and priority
If a thread enters the ready state, a thread scheduler is required to decide When to execute, schedule according to priority.
Thread group
Each thread is a member of a thread group. The thread group integrates multiple threads into one object. Through the thread group, multiple threads can be operated at the same time. When generating a thread, the thread must be placed in the specified
The thread group can also be placed in the default thread group. The default is the thread group where the thread that generated the thread is located. Once a thread joins a thread group, it cannot be removed from this group.
It is a special thread, generally used to provide services for other threads in the background.
isDaemon(): Determine whether a thread is a daemon thread.
set Daemon(): Set a thread as a daemon thread.
Thread class and Runnable interface
Thread class
Class Thread is defined in the package java.lang, and its construction method is as follows:
public Thread();
public Thread(Runnable target);
Public Thread(Runnable target, String name);
public Thread(String name);
public Thread(ThreadGroup group, Runnable target);
public Thread(ThreadGroup group, String name);
Main method
isActive() determines whether it is in execution state
Suspend() suspends execution
reSume resumes execution
start() starts execution
Stop() stops execution
sleep() sleep
run() program body
yield() yields running rights to other threads
thread priority
Public statuc final int MAX_PRIORITY highest priority, 10
Public statuc final int MIN_PRIORITY lowest priority ,1
Public statuc final int NORM_PRIORITY Normal priority, 5
Runnable interface
Only one method run() is defined in the Runnable interface as the thread body,
void run()
Java Threads are implemented through the java.lang.Thread class.
When VM starts, there will be a thread defined by the main method (public static void main(){}).
New threads can be created by creating instances of Thread.
Each thread completes its operation through the method run() corresponding to a specific Thread object. The method run() is called the thread body.
Start a thread by calling the start() method of the Thread class
There are two methods to implement multi-threading in Java
1. Inherit the Thread class, such as
class MyThread extends Thread {
public void run() {
// Write the content of the thread here
}
public static void main(String[] args) {
// Use this method to start a thread
new MyThread ().start();
}
}
2 Implement the Runnable interface
class MyThread implements Runnable{
public void run() {
// Write the content of the thread here
}
public static void main(String[] args) {
// Use this method to start a thread
new Thread(new MyThread()).start();
}
}
It is generally encouraged to use the second method because Java only allows single inheritance, but allows the implementation of multiple interfaces. The second method is more flexible.