When a thread is created and started, it neither enters the execution state as soon as it is started nor is it always in the execution state. During its life cycle, it has to go through five states: "New", "Runnable", "Running", "Blocked" and "Dead". After the thread is created, it is impossible for it to occupy the CPU and run independently. It needs to switch between multiple threads, so it is switched between running and blocking most of the time.
1. The state of the thread
There are several different states of the existence of a thread, as follows:
1. New status
The New state is a state where the thread has been created but has not started running. This state allows the thread to run by calling the start() method of the thread.
2. Runnable status
The Runnable state can be called the preparatory running state or the queue. This state can allow the thread to run by calling the start() method of the thread.
The thread scheduler determines which threads to run and how long the thread will run.
3. Running status
If a thread is executing, it is in the Running state.
4. Dead status
Once a thread enters the Dead state, it can no longer run.
5. Non runnable status
The Java virtual machine JVM executes threads according to thread priority and scheduling principles.
2. Thread Scheduler
In JVM, the implementation of thread scheduler is usually based on the following two strategies:
Preemptive scheduling strategy time-sharing scheduling strategy or Round-robin scheduling strategy
The implementation of a thread scheduler is platform-independent, so the scheduling of threads is unpredictable.
3. Priority of threads
The JVM assigns a priority to each newly created thread.
Level 0: This is the lowest priority
Level 5: This is the normal priority
Level 10: This is the highest priority
To save these values, the thread class has three corresponding variables:
A thread will first inherit the priority of its parent thread. The default priority of each thread is level 5 (Normal priority), and the default priority of the main thread is level 5.
The priority of the thread can be set through the setPriority(int priority) method.
public final void setPriority(int priority)
public void getPriority();
A user-defined thread has a default thread name Thread+ sequence number, and the sequence number starts from 0, such as the first thread is Thread0.
The thread name can be set through the setName(String name) method, and the thread name can be obtained using the getName() method.
public final void setName(String name)
public final String getName().
Example
Let's see an example below:
package demo.ch;public class UserThread extends Thread { UserThread() { super(); } UserThread(String name) { super(name); } public void run() { System.out.println("thread started running.."); } public static void main(String[] args) { UserThread thread1 = new UserThread("Thread1"); UserThread thread2 = new UserThread("Thread2"); System.out.println("Thread 1 initial name and priority"); System.out.println("name:" + thread1.getName()); System.out.println("priority:" + thread1.getPriority()); System.out.println("Thread 2 initial name and priority"); System.out.println("name:" + thread2.getPriority()); System.out.println("priority:" + thread2.getPriority()); System.out.println(""); thread1.setPriority(6); thread2.setPriority(9); System.out.println("Thread 1 initial name and priority"); System.out.println("name:" + thread1.getName()); System.out.println("priority:" + thread1.getPriority()); System.out.println("Thread 2 initial name and priority"); System.out.println("name:" + thread2.getName()); System.out.println("priority:" + thread2.getPriority()); System.out.println(""); thread1.start(); thread2.start(); for(int i=0; i<5; i++) System.out.println("main method i value: " + i); }}Output result:
Thread 1 initial name and priority name:Thread1priority:5Thread 2 initial name and priority name:Thread2priority:5Thread 1 initial name and priority name:Thread1priority:6Thread 2 initial name and priority name:Thread2priority:9main method i value: 0main method i value: 1thread started running..main method i value: 2thread started running..main method i value: 3main method i value: 4
Thank you for reading, I hope it can help you. Thank you for your support for this site!