I've made a small code for practising with Executors and Threads. It consists of the following:
- Create a fixed-thread pool of size 3 with an infinite queue.
- Submit 3 tasks with infinite loop (
while(true)
) to the pool (then all threads are occupied) - Submit a 4th task, which is going to be waiting in the queue.
- executor.shutdown() and doing a println for seeing how make active task and task count do i have.
- setting the flag to false in order to stop the infinite
while
and then doing a println for seeing how make active task and task count do i have - cancelling all futures with
mayInterruptIfRunning=true
and then doing a println for seeing how make active task and task count do i have
This is the code:
public class Main {
private static ThreadPoolExecutor fixexThreadPool;
public static void main(String[] args) throws InterruptedException {
System.out.println("Creating fixed thread pool of size 3 and infinite queue.");
fixexThreadPool = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
final Boolean[] flag = new Boolean[1];
flag[0] = true;
List<Future> futures = new ArrayList<>();
System.out.println("Submiting 3 threads");
for (int i = 0; i < 3; i++) {
futures.add(fixexThreadPool.submit(() -> {
int a = 1;
while (flag[0]) {
a++;
}
System.out.println("Finishing thread execution.");
}));
}
System.out.println("Done submiting 3 threads.");
System.out.println(String.format("Active count: %s | Completed task count: %s | task count: %s", fixexThreadPool.getActiveCount(), fixexThreadPool.getCompletedTaskCount(), fixexThreadPool.getTaskCount()));
Thread.sleep(3000L);
System.out.println("Submitting a 4th thread.");
futures.add(fixexThreadPool.submit(() -> {
int a = 1;
while (flag[0]) {
a++;
}
System.out.println("Finishing thread execution");
}));
System.out.println(String.format("Active count: %s | Completed task count: %s | task count: %s", fixexThreadPool.getActiveCount(), fixexThreadPool.getCompletedTaskCount(), fixexThreadPool.getTaskCount()));
System.out.println("Executor shutdown");
fixexThreadPool.shutdown();
System.out.println(String.format("Active count: %s | Completed task count: %s | task count: %s", fixexThreadPool.getActiveCount(), fixexThreadPool.getCompletedTaskCount(), fixexThreadPool.getTaskCount()));
Thread.sleep(2000L);
System.out.println("Setting flag to false.");
flag[0] = false;
Thread.sleep(2000L);
System.out.println(String.format("Active count: %s | Completed task count: %s | task count: %s", fixexThreadPool.getActiveCount(), fixexThreadPool.getCompletedTaskCount(), fixexThreadPool.getTaskCount()));
System.out.println("Cancelling all futures.");
futures.forEach(f -> f.cancel(true));
System.out.println(String.format("Active count: %s | Completed task count: %s | task count: %s", fixexThreadPool.getActiveCount(), fixexThreadPool.getCompletedTaskCount(), fixexThreadPool.getTaskCount()));
}
}
This is the output of the execution:
- Creating fixed thread pool of size 3 and infinite queue.
- Submiting 3 threads
- Done submiting 3 threads.
- Active count: 3 | Completed task count: 0 | task count: 3
- Submitting a 4th thread.
- Active count: 3 | Completed task count: 0 | task count: 4
- Executor shutdown
- Active count: 3 | Completed task count: 0 | task count: 4
- Setting flag to false.
- Active count: 3 | Completed task count: 0 | task count: 4
- Cancelling all futures.
- Active count: 3 | Completed task count: 0 | task count: 4
There are a couple of things i don't understand.
- Why, after shutting down executor, there still are active threads ?
- Why, after changing the flag to false in order to break the infinite loop, the infinite
while
doesn't break ? - Why, after cancelling every future, there is are active threads ?
- No matter if a change the flag to false, shutdown executor or even cancelling all futures, my program doesn't stop running. Why is that?
Thanks in advance!!