K
kermitisgreen
Gast
hallo miteinander.
habe ein kleines (wahrscheinlich verständnis)problem bezüglich der Thread.join() methode.
ich hab da ein paar threads, welche alle auf die beendigung eines bestimmten warten müssen. codemäßig hab ich das so gelöst (dh zu lösen probiert):
der aufruf:
das teil funktioniert aber leider nicht... was mach ich falsch?
lg kermit
habe ein kleines (wahrscheinlich verständnis)problem bezüglich der Thread.join() methode.
ich hab da ein paar threads, welche alle auf die beendigung eines bestimmten warten müssen. codemäßig hab ich das so gelöst (dh zu lösen probiert):
Code:
public class NewThread implements Runnable {
private boolean run = true;
private static int threadCount = 0;
private int countdown = 10;
private int threadnum;
// Thread auf den gewartet werden soll
private Thread watcha;
public Thread getWatcha() {
return watcha;
}
public void setWatcha(Thread watcha) {
this.watcha = watcha;
}
public NewThread() {
threadnum = threadCount++;
System.out.println("thread "+threadnum+" started");
}
public void run() {
while(run) {
if(watcha!=null) {
try {
watcha.join();
} catch(Exception e) {
e.printStackTrace();
}
}
System.out.print("thread "+threadnum+": "+countdown+", ");
if(watcha.isAlive()&&watcha!=null) {
System.out.println("watcha thread is alive");
} else if(watcha!=null){
System.out.println("watcha thread is dead");
}
countdown--;
if(countdown==0) {
run=false;
}
}
}
der aufruf:
Code:
public class Start {
/**
* @param args
*/
public static void main(String[] args) {
NewThread nt1 = new NewThread();
NewThread nt2 = new NewThread();
NewThread nt3 = new NewThread();
NewThread nt4 = new NewThread();
NewThread nt5 = new NewThread();
Thread t3 = new Thread(nt3);
nt1.setWatcha(t3);
nt2.setWatcha(t3);
nt4.setWatcha(t3);
nt5.setWatcha(t3);
Thread t1 = new Thread(nt1);
Thread t2 = new Thread(nt2);
Thread t4 = new Thread(nt4);
Thread t5 = new Thread(nt5);
t3.start();
t1.start();
t2.start();
t4.start();
t5.start();
}
}
das teil funktioniert aber leider nicht... was mach ich falsch?
lg kermit