This is the fifth day of my participation in the August Wen Challenge.More challenges in August

| author: jiangxia

| CSDN:blog.csdn.net/qq_41153943

| the nuggets: juejin. Cn/user / 651387…

| zhihu: www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

This article is about 1465 words. Read for 11 minutes

preface

A Thread has six states and can only be in one of these states at any one time. And these states can be represented by java.lang.thread. State:

The six specific states of a thread are as follows:

NEW

Status of threads that have not been started. When a new Thread is used, such as new Thread(r), but the start() has not yet been executed, the state of the Thread is new.

RUNNABLE

The thread state of a runnable thread. When the start() method is called, the thread enters the RUNNABLE state. The thread may or may not be running at this point

BLOCKED

The thread state of a thread that is blocked and waiting for a monitor lock.

A thread can be blocked in the following ways:

1. Wait for the return of an operation, such as an IO operation, and do not proceed with the following code until the operation returns.

2. Wait for a “lock” that will not continue until another thread or program releases the “lock”.

3. Wait for a trigger condition.

4. The thread executed the sleep method.

5. The thread is suspended by the suspend() method.

The same blocked thread can be reactivated in one of the following cases (even if the blocking condition is met) :

1. After executing the sleep() method, it is time to sleep.

2. The lock held by the waiting thread or program is released.

3. The thread is waiting for the trigger condition. The condition is met.

4. The suspend() method is executed, and the Resume () method is invoked.

5. The waiting operation returns to the thread, and the operation returns correctly.

WAITING

The thread status of the waiting thread. A Thread that cannot run because it called object.wait () or thread.join () is in a WAITING state.

TIMED_WAITING

The thread state of a waiting thread with a specified wait time. A Thread that is not running because it called thread.sleep () or called object.wait () or thread.join () with a timeout value is in TIMED_WAITING state.

TERMINATED

The thread state of a terminated thread. The thread has finished running. Its run() method ends normally or by throwing an exception.

The current specific state type can be obtained by using the thread.getState () method:

public class ThreadDemo{ public static void main(String[] args){ Thread t1 = new Thread(new Runnable(){ @Override public Void run() {// get the currentThread. System.out.println(" curthread.getState () "); // The current thread sleeps for 3 seconds try{curthread. sleep(3000); }catch(Exception e){system.out.println (" thread.getState () + thread.getState ()); }}}); System.out.println(" Status of thread not started; "+t1.getState()); t1.start(); System.out.println(" Thread status after startup; "+t1.getState()); Try {thread.sleep (5); } catch (InterruptedException e) { e.printStackTrace(); } t1.interrupt(); System.out.println(" Thread completion status; "+t1.getState()); }}Copy the code

Execution Result:

Waiting state and TimedWaiting state

public class ThreadDemo2{ public static Object object=new Object(); public static void main(String[] args){ Thread t = new Thread(new Runnable(){ @Override public void run() { Thread currThread = Thread.currentThread(); Println (String. Format (" Status of synchronized code blocks in %s :%s",currThread.getName(),currThread.getState())); Synchronized (object){try {//waiting state object.wait(); //time-wait state // object.wait(3000); } catch (InterruptedException e) { e.printStackTrace(); }}}}); Thread t1 = new Thread(t," Thread 1"); t1.start(); Println (string. format("%s status :%s",t1.getName(),t1.getState())); Threadt2 = new Thread(t," thread2 "); t2.start(); try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } system.out.println (string. format("%s status :%s",t2.getName(),t2.getState())); }}Copy the code

Execution Result:

 object.wait():

object.wait(3000):

Block and TimedWaiting:

public class ThreadDemo2{ public static Object object=new Object(); public static void main(String[] args){ Thread t = new Thread(new Runnable(){ @Override public void run() { Thread currThread = Thread.currentThread(); Println (String. Format (" Status of synchronized code blocks in %s :%s",currThread.getName(),currThread.getState())); //synchronized(object){// try {//waiting state // object.wait(); //time-wait state // object.wait(3000); //} catch (InterruptedException e) { // e.printStackTrace(); // } //} synchronized (object) { try { currThread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }}}}); Thread t1 = new Thread(t," Thread 1"); t1.start(); Println (string. format("%s status :%s",t1.getName(),t1.getState())); Threadt2 = new Thread(t," thread2 "); t2.start(); // If there is no sleep3 seconds, runnable try{thread.sleep (3000); }catch(InterruptedException e){ e.printStackTrace(); } system.out.println (string. format("%s status :%s",t2.getName(),t2.getState())); }}Copy the code

Execution result: The following two cases are true:

Output without sleep:

That’s all about the six states of threads!

Highlights from the past

  • Spring annotation (2) : @ComponentScan Automatically scans components
  • Spring is worth your collection!!
  • Spring annotation (7) : Assign attributes to beans using @value
  • SpringBoot develops Restful interfaces to implement CRUD functions
  • Introduction to distributed cache middleware Redis