[VC] thread. It is an entity in a process and the basic unit of independent scheduling and distribution by the system. a thread itself does not own system resources, but only has a little necessary resources in the operation, but it can be an entity in the same process, and is the basic unit of independent scheduling and distribution by the system. the thread itself does not own system resources, but only has a little necessary resources in the running process, however, it can share all the resources owned by the process with other threads of the same process. One thread can create and withdraw another thread, and multiple threads in the same process can execute concurrently. Due to mutual constraints between threads, threads are interrupted during running. The thread also has three basic states: Ready, blocked, and running.
A thread is a single sequential control process in a program. multiple threads run in a single program at the same time to complete different tasks, called multithreading.
The difference between a thread and a process is that the child process and the parent process have different code and data spaces, while multiple threads share the data space, each thread has its own execution stack and program counter for its execution context. multithreading is mainly used to save CPU time and make full use of it, depending on the specific situation. the computer's memory resources and CPU need to be used during thread running
Thread cycle
New ready to run blocking death
Thread scheduling and priority
When a thread enters the ready state, a thread scheduler is required to determine when to execute the task and schedule the task based on the priority.
Thread Group
Each thread is a member of a thread group. The Thread Group integrates multiple threads into one object. multiple threads can be operated simultaneously through the thread Group. when generating a thread, you must place the thread in the specified thread group or in the default Thread Group. the default Thread Group is the thread group where the thread that generates the thread is located. once a thread is added to a thread group, it cannot be removed from the group.
Daemon thread
It is a special thread and is generally used to provide services for other threads in the background.
IsDaemon (): determines whether a thread is a daemon.
Set Daemon (): sets a thread as the Daemon thread.
Thread class and Runnable interface
Thread class
The class Thread is defined in the java. lang package. its constructor 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 the execution status
Suspend () pause execution
ReSume execution of reSume
Start ()
Stop () Stop execution
Sleep () sleep
Run () program Body
Yield () revokes the running permission to other threads
Thread priority
Public statuc final int MAX_PRIORITY highest priority, 10
Public statuc final int MIN_PRIORITY minimum priority, 1
Public statuc final int NORM_PRIORITY normal priority, 5
Runnable interface
The Runnable interface only defines a method run () as the thread body,
Void run ()
Java threads are implemented through the java. lang. Thread class.
The VM starts with a thread defined by the main method (public static void main.
You can create a Thread by creating an instance of the Thread.
Each Thread performs operations 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.
Multiple threads are implemented in Java. There are two methods.
1. inherit the Thread class, such
Class MyThread extends Thread {
Public void run (){
// Write the thread content 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 thread content here
}
Public static void main (String [] args ){
// Use this method to start a thread
New Thread (new MyThread (). start ();
}
}
The second method is generally encouraged. Java only allows single inheritance, but multiple interfaces are allowed. The second method is more flexible.
...