Trouver le thread en cours d’exécution dans la machine virtuelle

Author:

classe, java
{filelink=10861}


  public class CurrentThread extends Thread
  	{
       private Thread thread_perso;  

       public CurrentThread()
       	{
          // Noter le thread crée par notre classe
           thread_perso = Thread.currentThread();
       }  

       public void run()
       	{
           for ( int i = 0; i < 10; i++ )
           {  

               trouver_thread();  

           }  

       }  

       // Trouver et afficher la référence de thread courant
       public void trouver_thread()
       	 {
           Thread t = Thread.currentThread();
           if ( t == thread_perso )
           	 {
               System.out.println("Thread Personnel en exécution");
           } else if ( t == this )
           	{
               System.out.println("Nouveau Thread en exécution");  

          } 

       }
       public static void main(String[] argv)
       	{
           CurrentThread thr = new CurrentThread();
           thr.start();
           for ( int i = 0; i < 10; i++ )
           {
               thr.trouver_thread();
           }  

       }  

   } 

Leave a Reply

Your email address will not be published. Required fields are marked *