/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.Date;
import java.util.concurrent.*;
public class Main {
public static void main(String[] args) throws InterruptedException {
int minThreadsInThePool = 1;
int maxThreadsInThePool = 4;
int keepAliveTimeMilliseconds = 0;
int maxItemsInTheQueue = 1;
ThreadPoolExecutor tpe = new ThreadPoolExecutor(
minThreadsInThePool,
maxThreadsInThePool,
keepAliveTimeMilliseconds,
TimeUnit.MILLISECONDS,
maxItemsInTheQueue == 0 ? new SynchronousQueue<>() : new ArrayBlockingQueue<>(maxItemsInTheQueue)
);
System.out.println("Starting adding new messages to pool");
for (int i = 0; i < 60; i++) {
tpe.submit(() -> {
try {
TimeUnit.MILLISECONDS.sleep(100);
System.out.println(new Date() + " Done! ");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
};
System.out.println("End of the program");
Thread.sleep(10_000);
}
}