This is the 28th day of my participation in the More Text Challenge. For more details, see more text Challenge
Related articles
Java Multithreading summary: Java multithreading
The status of the thread
-
New Threads that have not been started are in this state
-
Runnable threads executing in the Java VIRTUAL Machine are in this state
-
The thread waiting for a monitor lock is in this state.
-
Waiting In this state when a thread is Waiting for another thread to perform a specific action.
-
Timed Waiting A thread in this state is Waiting for another thread to perform an action.
-
The Terminated thread is in this state.
-
Code case
/** * Thread state */
public class TestThreadStatus{
public static void main(String[] args) throws InterruptedException {
// We use the lamda expression to start a thread
Thread th = new Thread(()->{
for (int i = 0; i <5 ; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("/ / / / / / / / / / / / / / / /");
});
Thread.State state = th.getState();
System.out.println("State of thread at creation +"+state);// Thread status
th.start();// Start the thread
state = th.getState();
System.out.println("State of the thread at start +"+state);
// Enter the thread state as long as the thread does not terminate
while(state ! = Thread.State.TERMINATED){ Thread.sleep(100);
state = th.getState();
System.out.println("new+"+state); }}}Copy the code
- The execution result is as follows:
Priority of the thread
-
Java provides a thread scheduler to monitor all threads in a program that enter the ready state after startup. The thread scheduler determines which threads should be scheduled for execution based on priority.
-
The priority of a thread is represented by a number ranging from 1 to 10.
-
Use the following methods to change or obtain the priority
- getPriority().setPriority(int xxx)
-
Code examples:
/** * Thread priority, not necessarily successful */
public class TestThreadPriority {
public static void main(String[] args) {
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority);
Thread t2 = new Thread(myPriority);
Thread t3 = new Thread(myPriority);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
Thread t6 = new Thread(myPriority);
// Set thread priority first
t1.setPriority(1);
t1.start();
t2.setPriority(3);
t2.start();
t3.setPriority(6);
t3.start();
t4.setPriority(Thread.MAX_PRIORITY);// Priority =10
t4.start();
t5.setPriority(Thread.MIN_PRIORITY);// Priority =1
t6.setPriority(9);
t6.start();
System.out.println("main"); }}class MyPriority implements Runnable{
@Override
public void run(a) {
System.out.println(Thread.currentThread().getName()+"-- The thread has been executed! -"+Thread.currentThread().getPriority()); }}Copy the code
- The execution result is as follows:
Third, daemon thread
-
Threads are divided into user threads and daemon threads
-
The VM must ensure that the user thread is finished
-
The VM does not have to wait for the daemon thread to finish executing
- For example, recording operation logs in the background, monitoring memory, garbage collection and so on…
-
Code case
public class TestDaemon {
public static void main(String[] args) {
God god = new God();
You you=new You();
Thread thread = new Thread(god);
thread.setDaemon(true);// The default is flase for user threads and true for daemon threads
thread.start();
newThread(you).start(); }}class God implements Runnable{
@Override
public void run(a) {
while (true){
System.out.println("God is watching over you -------"); }}}class You implements Runnable{
@Override
public void run(a) {
for (int i = 0; i <36500 ; i++) {
System.out.println("Happy to live every day ------");
}
System.out.println("----goodbye! Beautiful World!!! -- -- -- -- -- -"); }}Copy the code
- The execution result is as follows:
I see no ending, but I will search high and low
If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah