This is the program
public class Thread2 implements Runnable {
private static int runTill = 10000;
private static int count = 0;
@Override
public void run() {
for(int i=0;i<runTill;i++) {
count++;
}
}
public static void main(String s[]) {
int iteration = 10;
for(int i = 0; i < iteration ;i++) {
Thread t = new Thread(new Thread2());
t.start();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Expected : "+(iteration * runTill));
System.out.println("Actual : "+count);
}
}
At the end I want count to be equal to (Expected : 100000). How can I achieve this?
AtomicInteger.getAndIncrement(). docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/… Is there a reason why you are not using thejava.util.concurrentpackage API?Thread.join()to wait for their completion.Thread.join()on each thread, or use something like aCountDownLatch