When the thread is created and started, it either goes into execution or is not always executing, and in its lifecycle is new, Ready (Runnable), run (Running '), blocked (Blocked), and ".", and " The five states of Death (Dead). After the thread is created, it is not possible to occupy the CPU running independently, and it needs to switch between multiple threads, so most of the time is switched between running and blocking.
The state of a thread
There are several different states of the existence of threads, as follows:
- New state
- Ready state
- Running state
- Dead state
- Non runnable State
1, new state
The new state is the state in which the thread has been created, but has not yet started running. This state enables the thread to run by calling the thread's start () method.
2, runnable State
The runnable state can be called a ready to run state, or a queue, which enables the thread to run by calling the thread's start () method.
The thread scheduler determines which threads to run and how long the thread runs.
3, Running state
If a thread is executing, it is in a running state.
4, Dead State
Once a thread has entered the dead state, it is no longer able to run.
5, Non runnable state
- A running thread can be converted to a non runnable state, depending on the operation.
- A thread can also remain non runnable state until the condition that satisfies it appears.
- A non runnable state thread cannot jump directly to the running state, but must first be converted to a runnable state.
- Sleep sleeping: The time specified by the thread sleep.
- I/O blocking: The thread waits until the blocking operation completes.
- Join blocking: The thread waits until another thread finishes executing.
- Wait for notification: The thread waits for another thread to be notified.
- Lock mechanism blocking: the thread waits until the specified lock is freed and the lock is acquired.
The Java Virtual machine JVM executes threads based on the thread's priority and scheduling principles.
Second, the thread scheduler
In the JVM, the implementation of the thread scheduler is typically based on the following two strategies:
Preemptive scheduling strategy
Time-sharing scheduling strategy or round-robin cyclic scheduling strategy
The implementation of the thread scheduler is not platform-independent, so the scheduling of threads is unpredictable.
Third, the priority of the thread
The JVM assigns a priority to each newly created thread.
Level 0: This is the lowest priority
Level 5: It's a normal priority.
Level 10: This is the highest priority
To save these values, the thread class has three corresponding variables:
- public static final int min_priority
- public static final int norm_priority
- public static final int max_priority
A thread first inherits the priority of its parent thread, the default priority of each thread is level 5 (normal priority), and the main thread's default priority is level 5.
You can set the priority of a thread through the setpriority (int priority) method.
Public final void setpriority (int priority)
public void getpriority ();
A user-defined thread whose default thread name is Thread+ ordinal, with ordinal number starting at 0, such as the first thread being Thread0.
The thread name can be set through the SetName (String name) method, and the GetName () method is used to obtain the name of the thread.
Public final void SetName (String name)
Public final String GetName ().
Instance
Let's look at an example:
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.getname ());
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 results:
Thread 1 Initial name and priority
name:thread1
priority:5
thread 2 initial name and priority
Name: Thread2
priority:5
thread 1 Initial name and priority Name:thread1 priority:6
thread 2 Initial Name and priority
NAME:THREAD2
priority:9
Main method I value:0
Main method i value:1
thread s Tarted running.
Main method I value:2
thread started running.
Main method I value:3 the
main method I Value:4
Thank you for reading, I hope to help you, thank you for your support for this site!