Java examples for Thread:Lock
Put counter in synchronized block
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { private static int counter = 0; public static void increment() { for (int i = 0; i < 1000; i++) { synchronized (Main.class) { counter++;/* w w w . j a va2 s .c o m*/ } } } public static void main(String[] args) throws InterruptedException { long startTime = System.currentTimeMillis(); ExecutorService executor = Executors.newFixedThreadPool(2); for (int i = 0; i < 5; i++) { executor.execute(() -> { System.out.println(Thread.currentThread().getName()); increment(); }); } executor.shutdown(); while (!executor.isTerminated()) { } System.out.println("Finished all threads"); System.out.println("Counter: " + counter); long endTime = System.currentTimeMillis(); System.out.println("Total time taken(in millis): " + (endTime - startTime)); } }