[Java concurrent programming]-six States of a thread and their state conversion, and six States of a java thread
1 more content can view my another blog: http://blog.csdn.net/unikylin/article/details/45050823
1. Thread Information
The process of running a Thread generates a lot of information, which is stored in the member variables of the Thread class. Common information is as follows:
A. The thread ID is the unique identifier getId ()
B. Thread name: getName (). If the Thread name is not set, the default value is "Thread-xx"
C. Thread priority: getPriority. The thread priority ranges from 1 to 10. The larger the number, the higher the priority level. The more likely the JVM scheduling is to be executed at the same time. JDK has three built-in states:
1 // minimum Priority 2 public final static int MIN_PRIORITY = 1; 3 4 // general priority 5 public final static int NORM_PRIORITY = 5; 6 7 // maximum priority 8 public final static int MAX_PRIORITY = 10;
Generally, it is not recommended to set the thread priority. If an invalid priority is set, an IllegalArgumentException exception occurs.
2. Several States of the thread 1. There are six States of the Java thread
1 public enum State {2 // thread just created 3 NEW, 4 5 // thread 6 RUNNABLE in JVM running, 7 8 // thread in blocking status, WAITING for the monitoring lock, you can re-Execute 9 BLOCKED, 10 11 // wait Status 12 WAITING, 13 14 // call sleep () join () wait () method may cause the thread to be in the waiting status 15 TIMED_WAITING, 16 17 // The thread has been executed and has exited 18 TERMINATED; 19}
The preceding six statuses are shown as follows:
2. Explanation of thread status
1. When a Thread inherits the Thread or implements Runnable to create a Thread object, the Thread enters the initial state after the new Thread object.
2. When the thread object calls the start () method, the thread starts and enters the runable state.
3. after a thread enters the runnable State, if the logic is complete, the thread will end. If the JVM allocation time slice is used up before the execution is complete, the thread enters the runnable state, once the thread is selected by JVM, it is executed immediately.
4. The running status is complex.
First: If the thread completes the logic after running the run () main () method, the thread enters Terminated
Second: When the thread calls the sleep () or join () method, it enters the Blocked state. However, it should be noted that the Blocked thread does not release the system resources currently occupied. When sleep () end or join () waits for other threads to arrive, and the current thread enters the Runnable state to wait for the JVM to allocate resources.
Third: When the thread enters the Runnable State but has not started running, it finds that the required resources are in the synchronous state synchronized. At this Time, the thread will enter the Time waiting, the JVM uses a queue to control these threads. The thread that runs Time waiting first obtains JVM resources for execution and enters Waiting.
Fourth: If the Runnable thread calls yield () to let out JVM resources, it will enter the New State and compete with other New State threads to re-enter the Runnable state.
Fifth: If the current thread calls the wait () method, the current thread enters Time waiting. However, at this Time, the current thread will release the occupied JVM resources. After entering this state, it cannot automatically wake up, the notify () or notifyAll () method must be called, and the thread enters Waiting.
3. Case studies
Case: Use Cases to explain the six running states of a thread. The Pig class implements the Runnable interface. The logic is to print the information of the currently running thread and print it every second. Start ten Pig threads in the Main method to set the corresponding thread priority level, and save the initial thread status to the thread status array, during the running process, check whether the current thread status and initial status are the same. If they are different, print the information of the current thread and save it to the log file.
1 class Pig implements Runnable {2 3 @ Override 4 public void run () {5 for (int I = 0; I <10; I ++) {6 try {7 // The thread is sleeping for 8 TimeUnit seconds. SECONDS. sleep (1); 9} catch (InterruptedException e) {10 e. printStackTrace (); 11} 12 // print the information of the current execution thread 13 System. out. println ("ThreadName:" + Thread. currentThread (). getName (); 14} 15} 16} 17 18 public class App {19 public static void main (String [] args) throws Exception {20 // create a ready-made array 21 Thread [] taskArr = new Thread [10]; 22 23 // Thread status array 24 Thread. state [] threadStates = new Thread. state [10]; 25 26 // set the thread status 27 for (int I = 0; I <10; I ++) {28 taskArr [I] = new Thread (new Pig (); 29 30 // set status 31 if (I % 3) = 0) {32 taskArr [I]. setPriority (Thread. NORM_PRIORITY); 33} else if (I % 3) = 1) {34 taskArr [I]. setPriority (Thread. MIN_PRIORITY); 35} else if (I % 3) = 2) {36 ta SkArr [I]. setPriority (Thread. MAX_PRIORITY); 37} 38} 39 40 // write thread information to a file for analysis 41 FileWriter fWriter = new FileWriter (". \ log.txt "); 42 PrintWriter pWriter = new PrintWriter (fWriter); 43 44 // obtain thread information through loop traversal 45 for (int I = 0; I <10; I ++) {46 pWriter. println ("Thread" + I + "status:" + taskArr [I]. getState (); 47 48 // Save the current thread status to the status array 49 threadStates [I] = taskArr [I]. getState (); 50} 51 52 // start thread 53 for (int I = 0; I <10; I ++) {54 taskArr [I]. start (); 55} 56 57 // during running, if the thread status is different from the initial status, write the state change process to the file 58 boolean finish = false; 59 60 while (! Finish) {61 for (int I = 0; I <10; I ++) {62 // thread status change 63 if (taskArr [I]. getState ()! = ThreadStates [I]) {64 // print the current thread information 65 printThreadMsg (pWriter, taskArr [I], threadStates [I]); 66 67 // Save the current thread status to the thread status array 68 threadStates [I] = taskArr [I]. getState (); 69} 70} 71 finish = true; 72 for (int I = 0; I <10; I ++) {73 finish = finish & (taskArr [I]. getState () = State. TERMINATED ); 74} 75} 76} 77 78/** 79 * print information of the current thread 80 * @ param pWriter81 * @ param thread82 * @ param state83 */84 private static void printThreadMsg (PrintWriter pWriter, thread thread, State state) {85 pWriter. println ("************************************* ********************"); 86 pWriter. println ("thread ID:" + thread. getId () + "thread name:" + thread. getName (); 87 pWriter. println ("thread priority:" + thread. getPriority (); 88 pWriter. println ("past thread status:" + state); 89 pWriter. println ("current thread status:" + thread. getState (); 90 pWriter. println ("************************************* ********************"); 91} 92}
Partial execution results are as follows:
After analyzing the execution results, we can see that when the Pig thread is sleeping, the status of other threads will change, the previous status and current status can clearly reflect the thread status switching.