Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) throws Exception {
        BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(48);
        ThreadPoolExecutor testExecutor = new ThreadPoolExecutor(6, 10, 1, TimeUnit.SECONDS, blockingQueue);
        List<Future<String>> futures = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Future<String> testFuture = testExecutor.submit(new MyCallable(i));
            futures.add(testFuture);
        }
        for (Future<String> testFuture : futures) {
            System.out.println("Output Returned is : " + testFuture.get());
        }
    }
}

class MyCallable implements Callable<String> {
    int id;

    public MyCallable(int id) {
        this.id = id;
    }

    @Override
    public String call() throws Exception {
        System.out.println("Running " + this.id);
        return "Called " + this.id;
    }
}