Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
    int sum;

    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        Main app = new Main();
        for (int i = 0; i < 1000; i++) {
            pool.execute(app.new Adding());
        }
        pool.shutdown();
        while (!pool.isTerminated()) {
            System.out.println(" Is it done? : " + pool.isTerminated());
        }
        System.out.println(" Is it done? : " + pool.isTerminated());
        System.out.println("Sum is " + app.sum);
    }

    class Adding implements Runnable {
        public void run() {
            synchronized (Main.this) {
                sum += 1;
            }
        }
    }
}