Put counter in synchronized block - Java Thread

Java examples for Thread:Lock

Description

Put counter in synchronized block

Demo Code



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

}

Related Tutorials