the best solution is
thread1.join();
thread2.join();
Main Topics
Browse All TopicsHello,
I want to make a small program with two threads:
- one that add some numbers
- one that multiplies some numbers
In main I start both threads. How can I know when both threads are finished? (using notify and wait).
Thank You,
sey.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Or package tst;
public class produs extends Thread {
private Object monitor;
int max;
int p = 1;
public produs(int i, Object m){
max = i;
monitor = m;
}
public void run(){
for(int i = 1; i < max; i++){
p *= i;
System.out.println("i = " + i);
}
synchronized (monitor){
monitor.notify();
System.out.println("Produs
done = true;
}
}
private boolean done = false;
public boolean finished() {
return done;
}
}
package tst;
public class suma extends Thread {
private Object monitor;
int max;
int s = 0;
public suma(int i, Object m){
max = i;
monitor = m;
}
public void run(){
for(int i = 0; i < max; i++){
s += i;
}
synchronized (monitor){
monitor.notify();
System.out.println("Suma = " + s);
done = true;
}
}
private boolean done = false;
public boolean finished() {
return done;
}
}
package tst;
public class Test
{
public static void main(String [] args){
Object m = new Object();
Object m1 = new Object();
suma s = new suma(100, m);
produs p = new produs(100, m1);
p.start();
s.start();
try{
synchronized (m){
if (!p.finished())
m.wait();
}
synchronized(m1){
if (!s.finished())
m1.wait();
}
}catch(InterruptedExceptio
e.printStackTrace();
}
System.out.println("Total = " + (s.s + p.p));
}
}
Using join for such simple example is possible too but how to sinc many threads in many points (usual case of randevous). For this u can use Real Time Java API provided by many vendors.
Business Accounts
Answer for Membership
by: antonsPosted on 2002-01-09 at 06:17:47ID: 6720608
there is a lot of possibilities ...
for example lets the clas with the main method is named AA
then the main method after starting the both threads
executes the
synchronized(AA.class) {
AA.class.wait();
}
and AA implements the method
private static int numberOfFinished = 0;
public static synchronized wakeUp() {
numberOfFinished++;
if(numberOfFinished>1) AA.class.notify();
}
your working threads will call wakeUp() when they finish the work.