Recuperer une varible d un thread en java

Fermé
k-23 Messages postés 252 Date d'inscription mardi 4 mars 2008 Statut Membre Dernière intervention 25 novembre 2014 - 23 mai 2013 à 23:39
berrayahkamel Messages postés 174 Date d'inscription mardi 31 juillet 2007 Statut Membre Dernière intervention 6 avril 2020 - 13 juil. 2013 à 13:14
Bonjour,

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

vous avez des exemple

merci

A voir également:

1 réponse

berrayahkamel Messages postés 174 Date d'inscription mardi 31 juillet 2007 Statut Membre Dernière intervention 6 avril 2020 64
5 juil. 2013 à 11:15
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
berrayahkamel Messages postés 174 Date d'inscription mardi 31 juillet 2007 Statut Membre Dernière intervention 6 avril 2020 64
13 juil. 2013 à 13:14
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