Java thread's six states and examples

Java threads have six states since jdk1.5.

  • NEW: a thread that has not been started is in this state.
  • RUNNABLE: the thread executing in the Java virtual machine is in this state.
  • BLOCKED: the thread waiting for the monitor lock to be BLOCKED is in this state.
  • WAITING: a thread that waits indefinitely for another thread to perform a specific operation is in this state.
  • Timed "waiting: the thread waiting for another thread to perform the operation within the specified waiting time is in this state.
  • TERMINATED: the exited thread is in this state.

Source code in Thread class

public enum State {
    NEW,

    RUNNABLE,

    BLOCKED,

    WAITING,

    TIMED_WAITING,

    TERMINATED;
}

github address

Get Java thread six clock state sample demo

/**
 * 
 * Thread state
 * NEW(Initial): the thread state of a thread that has not been started.
 * RUNNABLE(Run): the thread state of the runnable thread.
 * BLOCKED(Blocking): thread state of thread is blocked, waiting for monitor lock.
 * WAITING(Wait): wait for the thread state of the thread.
 * TIMED_WAITING(Timeout wait): the thread state of the wait thread with the specified wait time.
 * TERMINATED(Terminate): terminates the thread state of the thread.
 * 
 * @version 1.0
 */
public class ThreadStatus {
	
	private static Object resource1 = "Resources 1";
	private static Object resource2 = "Resources 2";
	
	public static void main(String[] args) throws InterruptedException {
		ThreadStatusDemo t1 = new ThreadStatusDemo(resource1, resource2);
		t1.setName("Thread 1");
		System.out.println("Thread Name:"+t1.getName() + "-----Thread status:"+ t1.getState());
		t1.start();
		System.out.println("Thread Name:"+t1.getName() + "-----Thread status:"+ t1.getState());

		ThreadStatusDemo t2 = new ThreadStatusDemo(resource2, resource1);
		t2.setName("Thread 2");
		t2.start();
		Thread.sleep(1000);
		System.out.println("Thread Name:"+t2.getName() + "-----Thread status:"+ t2.getState());
		Thread.sleep(3000);
		System.out.println("Thread Name:"+t1.getName() + "-----Thread status:"+ t1.getState());
		
		final Object lock = new Object();
		Thread t3 = new Thread() {
			public void run() {
				synchronized (lock) {
					try {
						lock.wait();
					} catch (InterruptedException e) {
					}
				}
			}
		};
		Thread t4 = new Thread() {
			public void run() {
				synchronized (lock) {
					lock.notifyAll();
				}
			}
		};
		t3.setName("Thread 3");
		t4.setName("Thread 4");
		t3.start();
		t4.start();
		System.out.println("Thread Name:"+t3.getName() + "-----Thread status:"+ t3.getState());
		Thread.sleep(1000);
		System.out.println("Thread Name:"+t3.getName() + "-----Thread status:"+ t3.getState());
	}
	
}

class ThreadStatusDemo extends Thread {
	
	private Object resource1;
	private Object resource2;
	
	public ThreadStatusDemo(Object resource1, Object resource2) {
		this.resource1 = resource1;
		this.resource2 = resource2;
	}
	
	public void run() {
		synchronized (resource1) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			synchronized (resource2) {

			}
		}
	}
	
}

Output result

Thread Name: thread 1 -- thread status: NEW
 Thread Name: thread 1 -- thread status: RUNNABLE
 Thread Name: thread 2 -- thread status: timed "waiting
 Thread Name: thread 1 -- thread status: BLOCKED
 Thread Name: thread 3 -- thread status: WAITING
 Thread Name: thread 3 -- thread status: TERMINATED

 

Keywords: Programming Java github

Added by Anti-Moronic on Sun, 03 Nov 2019 00:13:12 +0200