Java Thread State

Posted by tinuviel on Thu, 02 Jul 2020 18:29:14 +0200

This article is as follows:

I. Several States and descriptions of Java threads
2. Sample Procedures

1. Status of Java Threads

The state of the Java thread can beJava.lang.ThreadObtained in.State, there are six states identified:

(1) NEW: Create a new thread, not yet started

(2) RUNNABLE: thread executing after calling start()

(3) BLOCKED: blocking state, waiting for lock release,
For example, thread A enters a synchronized method and thread B wants to enter this method, but the lock for this method has been acquired by thread A, at which point thread B is in the BLOCKED state.

(4) WAITING: The thread invoked the following methods:
1)Object.waitWith no timeout Note: lock needs to be acquired first when using wait() method, otherwise IllegalMonitorLock exception will be reported
2)Thread.join with no timeout
3)LockSupport.park
For example, thread A calls Object.wait() Method waits for another thread B callObject.notify() orObject.notifyAll() wake it up; or a thread A calls it Thread.join() Wait for another thread to terminate it.

(5) TIMED_WAITING: The thread invoked the following methods:
1) Thread.sleep
2)Object.waitWith timeout//Wait for other threads to call notify to wake up the thread or automatically wake up after the timeout has elapsed
3) Thread.join with timeout
4) LockSupport.parkNanos
5) LockSupport.parkUntil

(6) TERMINATED: thread execution complete

Sample program for the above thread state

public class ThreadStateSwitch {
    static Object object = new Object();

    @Test
    public void test() {
        //-----------------------------------------------
        //Create thread in NEW state
        Thread thread = new Thread(new NewAndRunnable());
        System.out.println(thread.getState());

        //-----------------------------------------------
        //Execution thread in RUNNABLE state
        thread.start();
        System.out.println(thread.getState());

        //-----------------------------------------------
        //Call wait() method in WITING state
        Thread waitingThread = new Thread(new WaitingState());
        waitingThread.start();
        sleepMainThread(3000);

        //Call the notify() method to wake up the thread
        Thread notifyThread = new Thread(new NotifyThread());
        notifyThread.start();
        System.out.println(waitingThread.getName() + " " + waitingThread.getState());
        sleepMainThread(3000);
        //-----------------------------------------------
        //Call wait() method in TIME_stateWITING
        Thread timeWaitingThread = new Thread(new TimeWaitingThread());
        timeWaitingThread.start();
        sleepMainThread(6000);

        //-----------------------------
        //Call method with synchronized status BLOCKED
        Thread blockThreadA = new Thread(new BlockThreadA());
        Thread blockThreadB = new Thread(new BlockThreadB());
        blockThreadA.start();
        blockThreadB.start();
        System.out.println("blockThreadA " + blockThreadA.getState() + "blockThreadB " + blockThreadB.getState());
        sleepMainThread(3000);
    }


    class NewAndRunnable implements Runnable {
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " NewAndRunnable is running");
        }
    }


    //Note that synchronization locks need to be acquired when wait is called
    class WaitingState implements Runnable {
        @Override
        public void run() {
            synchronized (object) {
                try {
                    object.wait();
                    for (int i = 0; i < 4; i++) {
                        System.out.println(i + " ");
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    class NotifyThread implements Runnable {

        @Override
        public void run() {
            synchronized (object) {
                object.notify();
                System.out.println(Thread.currentThread().getName() + " " + Thread.currentThread().getState() + " notify thread");
            }
        }
    }

    class TimeWaitingThread implements Runnable {
        int i = 0;

        @Override
        public void run() {
            synchronized (object) {
                try {
                    object.wait(3000);
                    while (i < 5) {
                        System.out.println(i + "--");
                        i++;
                    }
                    Thread.sleep(1000l);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void sleepMainThread(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    class BlockThreadA implements Runnable {

        @Override
        public void run() {
            block();
        }
    }

    class BlockThreadB implements Runnable {

        @Override
        public void run() {
            block();
        }
    }

    private synchronized void block() {
        int i = 0;
        while (i < 5) {
            System.out.println(i);
            i++;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

Topics: Java