Home >Java >javaTutorial >Introduction to the life cycle of Java threads (with examples)
This article brings you an introduction to the life cycle of Java threads (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
1. Thread life cycle
Processes, like threads, have a certain life cycle. The thread life cycle includes four states: creation state, ready state, blocking state, and death state. .
1. Creation state
1) refers to using new to instantiate a thread object, but the thread object has not yet used the start() method to start the thread. This stage is only in the memory. The memory space is allocated for the instance variable of the object in the heap, but the thread cannot participate in grabbing the right to use the CPU;
2) After the thread object is created, the start() method is used to start the thread object, and Not the run() method.
2. Ready state
1) refers to the stage from when a thread object uses the start() method to finishing running the run() method. Once the thread enters the ready stage, the Java virtual machine Create the call stack and counter of the method for the thread;
2) Within a certain unit time (time slice), the CPU can only run one thread. Once a thread has the right to use the CPU, then This thread can also be called the running state;
3) All threads in the ready state are considered active. You can use the isAlive() method to test whether the thread is in the ready state, and use activeCount() to query the current The number of active threads in the thread pool where the thread is located;
4) The thread in the ready state is not in the running state. In the past, many computers were single-processor, and all threads in the ready state must be run at the same time. It is impossible. Java uses some scheduling algorithms to ensure that these threads share the use of the processor (such as time slice rotation algorithm, exclusive algorithm, etc.).
3. Blocking state:
1) The blocking state includes four states (sleep state, blocking state, suspend state, waiting state). Generally speaking, the blocking state and The ready state can be switched between each other;
2) Use the sleep() method to put the thread into sleep state, allowing other processes to get a chance to run, but using the sleep method must capture the InterruptedExecption exception;
3) You can use the suspend method to suspend a thread (obsolete after jdk1.2), use the wait method to put a thread into a waiting state (there will be an essay dedicated to it later), and use I/O interrupts to put a thread into a blocking state.
4. Death state:
1) Once the thread finishes running the run method, the thread enters the death state, and the Java virtual machine destroys the system resources occupied by the thread object in the death state;
2) When the thread encounters an uncaught exception during execution, the thread will be terminated and enter the death state; calling the stop method can also cause the thread to enter the death state, but it is easy to cause deadlock and has been deprecated.
5. The thread life cycle is as follows:
2. The following is a case where the sleep method puts the thread into sleep state
/** * @author: PrincessHug * @date: 2019/4/12, 9:20 * @Blog: https://www.cnblogs.com/HelloBigTable/ */ public class SleepDemo implements Runnable{ @Override public void run() { long l; for (int i=1;i<6;i++){ l = System.currentTimeMillis(); try { Thread.currentThread().sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } l = (System.currentTimeMillis() - l); System.out.println(Thread.currentThread().getName() + "线程执行了" + i + "次,耗时" + l + "毫秒。"); } } } public class SleepDriver { public static void main(String[] args) { SleepDemo sd = new SleepDemo(); for (int i=0;i<50;i++){ new Thread(sd,i + "#").start(); } } }
The following are some screenshots of the running results:
You can see if at the same time The more threads started, the longer each thread will take.
The above is the detailed content of Introduction to the life cycle of Java threads (with examples). For more information, please follow other related articles on the PHP Chinese website!