Home >Java >javaTutorial >How to check thread running status in Java

How to check thread running status in Java

WBOY
WBOYforward
2023-05-03 22:31:052975browse

1. Check the running status of the thread

Question

Threads have the following 6 states: new, running, blocked, waiting, timed waiting and terminated.

When new a new thread is created, the thread is in the newly created state.

When the start() method is called, the thread is in a running state.

When a thread needs to obtain the object's built-in lock, and the lock is owned by another thread, the thread is blocked.

When a thread is waiting for other threads to notify the scheduler that the scheduler can run, the thread is in a waiting state.

For some methods that contain time parameters, such as the sleep() method of the Thread class, the thread can be placed in a timing waiting state.

When the run() method finishes running or an exception occurs, the thread is in a terminated state.

Implementation: View the running status of the thread.

2. Problem-solving ideas

Create a class: ThreadState, implement the Runnable interface

Define 3 methods:

  • waitForASecond (): Make the current thread wait for 0.5 seconds or other threads call notify() or notifyAll() method

  • waitForYears(): Make the current thread wait forever until other threads call notify() Or notifyAll() method

  • notifyNow(): wake up the thread that entered the waiting state by calling the wait() method

Use getState of the Thread class () method to obtain the status of the thread.

The return value of this method is Tread.State

3. Detailed code explanation

package com.xiaoxuzhu;

/**
 * Description: 
 *
 * @author xiaoxuzhu
 * @version 1.0
 *
 * <pre class="brush:php;toolbar:false">
 * 修改记录:
 * 修改后版本	        修改人		修改日期			修改内容
 * 2022/5/10.1	    xiaoxuzhu		2022/5/10		    Create
 * 
 * @date 2022/5/10  */ public class ThreadState implements Runnable {     public synchronized void waitForASecond() throws InterruptedException {         wait(500); // 使当前线程等待0.5秒或其他线程调用notify()或notifyAll()方法     }     public synchronized void waitForYears() throws InterruptedException {         wait(); // 使当前线程永久等待,直到其他线程调用notify()或notifyAll()方法     }     public synchronized void notifyNow() throws InterruptedException {         notify(); // 唤醒由调用wait()方法进入等待状态的线程         Thread.sleep(100);//留时间打印     }     public void run() {         try {             waitForASecond(); // 在新线程中运行waitForASecond()方法             waitForYears(); // 在新线程中运行waitForYears()方法         } catch (InterruptedException e) {             e.printStackTrace();         }     } }

Test class:

package com.xiaoxuzhu;

/**
 * Description:
 *
 * @author xiaoxuzhu
 * @version 1.0
 *
 * <pre class="brush:php;toolbar:false">
 * 修改记录:
 * 修改后版本	        修改人		修改日期			修改内容
 * 2022/5/10.1	    xiaoxuzhu		2022/5/10		    Create
 * 
 * @date 2022/5/10  */ public class Test {     public static void main(String[] args) throws InterruptedException {         ThreadState state = new ThreadState();// 创建State对象         Thread thread = new Thread(state);// 利用State对象创建Thread对象         System.out.println("新建线程:" + thread.getState());// 输出线程状态         thread.start(); // 调用thread对象的start()方法,启动新线程         System.out.println("启动线程:" + thread.getState());// 输出线程状态         Thread.sleep(100); // 当前线程休眠0.1秒,使新线程运行waitForASecond()方法         System.out.println("计时等待:" + thread.getState());// 输出线程状态         Thread.sleep(1000); // 当前线程休眠1秒,使新线程运行waitForYears()方法         System.out.println("等待线程:" + thread.getState());// 输出线程状态         state.notifyNow(); // 调用state的notifyNow()方法         System.out.println("唤醒线程:" + thread.getState());// 输出线程状态         Thread.sleep(1000); // 当前线程休眠1秒,使新线程结束         System.out.println("终止线程:" + thread.getState());// 输出线程状态     } }

How to check thread running status in Java

The above is the detailed content of How to check thread running status in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete