L
lonelyplanet999
Hi,
I'm writing a program using 3 threads. I want to make thread a to join
thread b after it runs for some time but before both terminates. I
want to put the join method inside run() but I find it not possible to
make the JoinThread class know what the identities of thread a & b.
If I put b.join inside main(), it seems the original idea will be
violated i.e. it's main which join thread b rather than thread a.
I would like to ask how can I modify the code to achieve my original
goal ?
Tks
public class Join {
public static void main (String [] args) {
JoinThread jt = new JoinThread();
Thread a = new Thread(jt);
a.setName("a");
Thread b = new Thread(jt);
b.setName("b");
Thread c = new Thread(jt);
c.setName("c");
c.setPriority(1);
a.start();
b.start();
c.start();
try {
b.join();
} catch (InterruptedException e) {}
}
}
class JoinThread implements Runnable {
public void run() {
for (int i=0; i<10; i++) {
System.out.println("Thread."+Thread.currentThread().getName()+"
is running");
}
}
}
I'm writing a program using 3 threads. I want to make thread a to join
thread b after it runs for some time but before both terminates. I
want to put the join method inside run() but I find it not possible to
make the JoinThread class know what the identities of thread a & b.
If I put b.join inside main(), it seems the original idea will be
violated i.e. it's main which join thread b rather than thread a.
I would like to ask how can I modify the code to achieve my original
goal ?
Tks
public class Join {
public static void main (String [] args) {
JoinThread jt = new JoinThread();
Thread a = new Thread(jt);
a.setName("a");
Thread b = new Thread(jt);
b.setName("b");
Thread c = new Thread(jt);
c.setName("c");
c.setPriority(1);
a.start();
b.start();
c.start();
try {
b.join();
} catch (InterruptedException e) {}
}
}
class JoinThread implements Runnable {
public void run() {
for (int i=0; i<10; i++) {
System.out.println("Thread."+Thread.currentThread().getName()+"
is running");
}
}
}