public class ThreadA {
public static void main(String[] args){
ThreadB b = new ThreadB();
b.start();
synchronized(b){
try{
One of the many possible solutions for your problem is:
public class ThreadA {
public static final CyclicBarrier barrier = new CyclicBarrier(2);
public static void main(String[] args) {
ThreadB b = new ThreadB();
b.start();
try {
barrier.await();
System.out.println("Total is: " + b.total);
} catch (InterruptedException | BrokenBarrierException ex) {
}
}
}
class ThreadB extends Thread {
int total;
@Override
public void run() {
for (int i = 0; i < 100; i++) {
total += i;
}
try {
ThreadA.barrier.await();
} catch (InterruptedException | BrokenBarrierException ex) {
}
}
}