Maison  >  Article  >  Java  >  Comment vérifier l'état d'exécution du thread en Java

Comment vérifier l'état d'exécution du thread en Java

WBOY
WBOYavant
2023-05-03 22:31:052906parcourir

1. Vérifiez l'état d'exécution du fil de discussion

Question

Les fils de discussion ont les 6 états suivants : nouveau, en cours d'exécution, bloqué , et attente, attente chronométrée et terminaison.

Lorsqu'un nouveau fil de discussion est créé, le fil de discussion est dans l'état nouvellement créé.

Lorsque la méthode start() est appelée, le thread est en cours d'exécution.

Lorsqu'un thread doit obtenir le verrou intégré d'un objet et que le verrou appartient à un autre thread, le thread est bloqué.

Lorsqu'un thread attend que d'autres threads informent le planificateur qu'il peut s'exécuter, le thread est dans un état d'attente.

Pour certaines méthodes qui contiennent des paramètres temporels, comme la méthode sleep() de la classe Thread, le thread peut être placé dans un état d'attente de synchronisation.

Lorsque la méthode run() termine son exécution ou qu'une exception se produit, le thread est dans un état terminé.

Implémentation : Vérifiez l'état d'exécution du fil de discussion.

2. Idées de résolution de problèmes

Créer une classe : ThreadState, implémenter l'interface Runnable

Définir 3 méthodes :

#🎜 🎜 #
  • waitForASecond() : Faites attendre le thread actuel pendant 0,5 seconde ou d'autres threads appellent la méthode notify() ou notifyAll()

  • waitForYears() : Fait attendre indéfiniment le thread actuel jusqu'à ce que d'autres threads appellent la méthode notify() ou notifyAll() state en appelant la méthode wait() Thread

  • Utilisez la méthode getState() de la classe Thread pour obtenir l'état du thread.

    La valeur de retour de cette méthode est Tread.State
3 Explication détaillée du code

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(); } } }

Classe de test :

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());// 输出线程状态 } }#🎜 🎜#

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer