Thread state
Threads have five states: creation state, ready state, running state, blocking state and death state.
The relationship between the five states is as follows:
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-bglli6vo-1628757076537) (C: \ users \ Zhou \ appdata \ roaming \ typora \ user images \ image-20210812150748866. PNG)]
Stop thread
-
The stop() and destroy() methods provided by JDK are not recommended. [abandoned]
-
It is recommended that the thread stop itself
-
It is recommended to use a flag bit to terminate the variable. When flag=false, the thread will be terminated.
Code implementation:
public class TestStop implements Runnable{ private Boolean flag = true; @Override public void run() { int i = 0; while (flag) { System.out.println("run---Thread" + i++); } } public void stop() { this.flag = false; } public static void main(String[] args) { TestStop testStop = new TestStop(); new Thread(testStop).start(); for (int i = 0; i < 1000; i++) { System.out.println("main" + i); if (i == 900) { testStop.stop(); System.out.println("The thread should stop"); } } } }
Thread sleep
-
Sleep (time) specifies the number of milliseconds that the current thread is blocked;
-
Exception InterruptedException exists in sleep;
-
After the sleep time reaches, the thread enters the ready state;
-
sleep can simulate network delay, countdown, etc.
sleep analog print current system time:
//Print system current time Date startTime = new Date(System.currentTimeMillis()); //Get the current system time while (true) { try { Thread.sleep(1000); System.out.println(new SimpleDateFormat("HH:mm:ss").format(startTime)); startTime = new Date(System.currentTimeMillis()); //Update current time } catch (InterruptedException e) { e.printStackTrace(); } }
sleep simulation Countdown:
//Analog countdown public static void tenDown() throws InterruptedException { int num = 10; while (true) { Thread.sleep(1000); System.out.println(num--); if (num <= 0){ break; } } }
-
Each object has a lock, and sleep will not release the lock;
Thread comity
- Comity thread, which suspends the currently executing thread without blocking
- Transition a thread from a running state to a ready state
- Let the cpu reschedule. Comity may not be successful. It depends on the cpu mood
Code implementation:
public class TestYield { public static void main(String[] args) { MyYield myYield = new MyYield(); new Thread(myYield, "a").start(); new Thread(myYield, "b").start(); } } class MyYield implements Runnable{ @Override public void run() { System.out.println(Thread.currentThread().getName() + "Thread starts execution"); Thread.yield(); //Comity System.out.println(Thread.currentThread().getName() + "Thread stop execution"); } }
Comity success:
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-gtxeajz9-1628757076539) (C: \ users \ Zhou \ appdata \ roaming \ typora \ user images \ image-20210812155817855. PNG)]
Comity failed:
[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-m2itzxuf-1628757076539) (C: \ users \ Zhou \ appdata \ roaming \ typora \ user images \ image-20210812155945065. PNG)]
Join
- Join merge threads. After this thread is executed, execute other threads. Other threads are blocked
- Imagine jumping in line
Code implementation:
public class TestJoin implements Runnable{ @Override public void run() { for (int i = 0; i < 300; i++) { System.out.println("thread vip coming" + i); } } public static void main(String[] args) throws InterruptedException { //Start our thread TestJoin testJoin = new TestJoin(); Thread thread = new Thread(testJoin); thread.start(); //Main thread for (int i = 0; i < 1000; i++) { if (i == 60){ thread.join();//Jump in line } System.out.println("main"+i); } } }
Thread state observation
Thread.State
Thread status. A thread can be in one of the following states:
-
NEW
Threads that have not been started are in this state
-
RUNNABLE
The thread executing in the Java virtual machine is in this state
-
BLOCKED
Threads that are blocked waiting for a monitor lock are in this state
-
WAITTING
A thread that is waiting for another thread to perform a specific action is in this state
-
TIMED_WAITTING
The thread that is waiting for another thread to perform the action for the specified waiting time is in this state
-
TERMINATED
The exited thread is in this state