preface

If a knowledge point, learn 10 times still can not!! That study dozens of times, hundreds of times…… If still can’t learn, that I don’t believe!!

The life cycle of a thread, as well as the transitions between several states and states of a thread, is pretty much anyone’s guess, but the transitions between each state, and which methods can be called from which state to which state, are somewhat obscure. This time round it!!

Several states of a thread

A more detailed division, there are a total of 6 states of threads, respectively:

  • New
  • Runnable
  • Blocked
  • Waiting (no time limit)
  • Time Waiting (with a deadline)
  • Terminated

Thread 6 clock state changes, look at the picture and speak, a picture is worth a thousand words

Code debugging

Prints NEW, RUNNABLE, and TERMINATED states

code

/ * * *@Description:
 * @author: ListenerSun(male, single) wechat :810548252 *@Date: Created in 2021-04-23 0:55
 */
public class ThreadLifeCycle implements  Runnable{
    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadLifeCycle());
        // Print out the NEW state
        System.out.println(thread.getState());
        thread.start();
        // Print out Runnable
        System.out.println(thread.getState());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // Prints out the TERMINATED state
        System.out.println(thread.getState());
    }
    public void run(a) {
        for (int i = 0; i < 1000; i++) {

        }
    }
}
Copy the code

Print the result

The status of Time Waiting, Waiting, and BLOCKED is displayed

Because multithreaded code is not easy to debug, you can open your own multithreaded debugging according to the comments in the code to print out the thread of each state!!

/ * * *@Description:
 * @author: ListenerSun(male, single) wechat :810548252 *@Date: Created in 2021-04-23 1:08
 */
public class ThreadLifeCycle1 implements Runnable {

    public static void main(String[] args) {
        ThreadLifeCycle1 runable = new ThreadLifeCycle1();
        Thread thread1 = new Thread(runable);
        thread1.start();
        Thread thread2 = new Thread(runable);
        thread2.start();
        Thread1 is executing thread.sleep (10000), so print TIMED_WAITING
        System.out.println(thread1.getState());
        try {
            Thread.sleep(1300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Thread1 prints Waiting because it is executing wait()
        System.out.println(thread1.getState());
        Thread2 prints BLOCKED because it can't get sync's lock
        System.out.println(thread2.getState());
// Thread.sleep();
    }
    public void run(a) {
        sync();
    }
    private synchronized void sync(a) {
        try {
            Thread.sleep(10000);
            wait();
        } catch(InterruptedException e) { e.printStackTrace(); }}}Copy the code

conclusion

The above is the various states of the thread, and which methods will trigger the thread conversion, summed up very comprehensive!!

Generally, Blocked, Waiting, and Time Waiting states are called Blocked, not just Blocked

The more you know, the more you don’t know

If you like, please pay attention to my public number [program listener] and tell your story! I am here to listen! Text address