ExecutorService executor = Executors.newFixedThreadPool(5); // Submit some tasks to the executor for (int i = 0; i < 10; i++) { executor.submit(new MyTask()); } // Shutdown the executor and prevent any new tasks from being submitted ListunfinishedTasks = executor.shutdownNow();
ExecutorService executor = Executors.newCachedThreadPool(); // Submit some tasks to the executor for (int i = 0; i < 100; i++) { executor.submit(new MyTask()); } // Shutdown the executor and prevent any new tasks from being submitted ListIn this example, we create a new cached thread pool and submit 100 tasks to the executor. We then call shutdownNow() to terminate all actively executing tasks and prevent any new tasks from being submitted. The method returns a list of any tasks that were submitted but not started. Overall, java.util.concurrent is a package library in Java that provides powerful tools for working with concurrent threads, including the ExecutorService class and its shutdownNow() method.unfinishedTasks = executor.shutdownNow();