Recuperer une varible d un thread en java

k-23 Messages postés 256 Date d'inscription   Statut Membre Dernière intervention   -  
berrayahkamel Messages postés 175 Date d'inscription   Statut Membre Dernière intervention   -
Bonjour,

je voudrais savoir si c possible de récupérer une variable d<un thread

vous avez des exemple

merci

1 réponse

  1. berrayahkamel Messages postés 175 Date d'inscription   Statut Membre Dernière intervention   64
     
    voila un eemple simple :

    class UnThread extends Thread{

    public int mavar;
    public void run() {
    long start = System.currentTimeMillis();
    // boucle tant que la durée de vie du Thread est < à 5 secondes
    while( System.currentTimeMillis() < ( start + (1000 * 5))) {
    // traitement
    System.out.println("Ligne affichée par le thread");
    try {
    // pause
    mavar++;
    Thread.sleep(500);
    }
    catch (InterruptedException ex) {}
    }
    }
    }

    class runThread{
    public static void main (String[] args) {

    UnThread[] tab= new UnThread[10] ;

    tab[i] = new UnThread();
    tab[i].mavar =30;
    System.out.println(" taille tableau " + tab.length);
    // création d'une instance du Thread
    UnThread thread = new UnThread();
    // Activation du Thread
    //thread.start();
    tab[1].start();
    // tant que le thread est en vie...
    while( tab[1].isAlive() ) {
    // faire un traitement...
    System.out.println("Ligne affichée par le main");
    try {
    // et faire une pause
    tab[i].mavar++;
    tab[1].sleep(800);
    }
    catch (InterruptedException ex) {}
    }
    }

    }
    1
    1. berrayahkamel Messages postés 175 Date d'inscription   Statut Membre Dernière intervention   64
       
      voila un autre exemple simple
      la variable en question est : int numero

      import java.io.DataOutputStream;

      class UnThread extends Thread{

      DataOutputStream is,os;
      int numero ;

      UnThread(DataOutputStream is,DataOutputStream os)
      {
      this.is = is;
      this.os =os;
      }

      public UnThread(int i) {

      numero = i;
      // TODO Auto-generated constructor stub
      }

      public void run() {
      long start = System.currentTimeMillis();
      // boucle tant que la durée de vie du Thread est < à 5 secondes
      while( System.currentTimeMillis() < ( start + (100000 * 5))) {
      // traitement
      System.out.println("Ligne affichée par le thread numero: " + numero);
      try {
      // pause
      Thread.sleep(500);
      }
      catch (InterruptedException ex) {}
      }

      }
      }


      class runThread{
      public static void main (String[] args) {
      // création d'une instance du Thread
      UnThread thread1 = new UnThread(1);
      UnThread thread2 = new UnThread(2);
      // Activation du Thread
      thread1.start();
      thread2.start();
      // tant que le thread est en vie...
      while( thread1.isAlive() || thread2.isAlive() ) {
      // faire un traitement...
      System.out.println(" Il y a un thread en cour d'execution");
      try {
      // et faire une pause
      thread1.sleep(800);
      thread2.sleep(50);



      }
      catch (InterruptedException ex) {}
      }
      }



      }
      0