How to control one thread to join another while it's running

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");
}
}
}
 
J

John C. Bollinger

lonelyplanet999 said:
I'm writing a program using 3 threads.

Your code actually has 4 threads, though, once you include the main thread.
I want to make thread a to join
thread b after it runs for some time but before both terminates. I

That doesn't make much sense to me. Joining a thread means waiting
until that thread terminates (if it hasn't already). It isn't very
relevant if the joinee has already stopped running, and it can't be done
if the joiner is not running.
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.

Since run() takes no parameters, if you want to communicate information
to it then you must use its instance members (set via a constructor, or
by some other means), static members of any class, members of a
containing instance, or some form of I/O. (Sorry the choices are so
limited. :) )
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.

Quite right.
I would like to ask how can I modify the code to achieve my original
goal ?

The above alternatives should give you some ideas.
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 {

Ack! Pffthhth.

This class is not a Thread. Do not name it as if it were.

You do get some points back for using a Runnable to define your threads'
work instead of subclassing Thread, however.
public void run() {
for (int i=0; i<10; i++) {
System.out.println("Thread."+Thread.currentThread().getName()+"
is running");
}
}
}


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top