package com.example.lambda;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorExample {

	public static void main(String[] args) {
		// define an executor service object with pool size = 3
		ExecutorService executor = Executors.newFixedThreadPool(3);

		// define the runnable objects (task to be performed)
		Runnable task1 = () -> System.out.println("Task 1 executed");
		Runnable task2 = () -> System.out.println("Task 2 executed");
		
		// submit the tasks to executor to be executed
		executor.submit(task1);
		executor.submit(task2);
		executor.submit(new MyRunnable());
		executor.submit(task1);
		executor.submit(task2);
		executor.submit(new MyRunnable());
		
		// just to ensure executor is shutdown after all tasks completed
		executor.shutdown();
		
		executor.submit(task1); // will not work, error
	}

}

class MyRunnable implements Runnable {

	@Override
	public void run() {
		int total = 0;
		for (int i = 0; i < 5; i++) {
			total += i;
		}
		System.out.println("Total is " + total);
	}
	
}