public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new ArrayBlockingQueue<>(2), new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 0; i < 7; i++) {
int finalI = i;
executor.execute(() -> {
System.out.println("prepare to execute thread i:"+ finalI );
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
System.out.println("finish to execute thread " + finalI );
public static void main(String[] args) throws InterruptedException {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 3, TimeUnit.SECONDS, new SynchronousQueue<>(), new ThreadPoolExecutor.DiscardOldestPolicy());
for (int i = 0; i < 6; i++) {
int finalI = i;
executor.execute(() -> {
System.out.println("prepare to execute thread i:"+ finalI );
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
System.out.println("finish to execute thread " + finalI );