ExecutorService executor = Executors.newSingleThreadExecutor(); executor.submit(() -> System.out.println("Task 1")); executor.submit(() -> System.out.println("Task 2")); executor.submit(() -> System.out.println("Task 3")); executor.shutdown();
ExecutorService executor = Executors.newCachedThreadPool(); executor.submit(() -> System.out.println("Task 1")); executor.submit(() -> System.out.println("Task 2")); executor.submit(() -> System.out.println("Task 3")); executor.shutdown();
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); executor.schedule(() -> System.out.println("Task 1"), 5, TimeUnit.SECONDS); executor.schedule(() -> System.out.println("Task 2"), 10, TimeUnit.SECONDS); executor.schedule(() -> System.out.println("Task 3"), 15, TimeUnit.SECONDS); executor.shutdown();The package library used in the above examples is `java.util.concurrent`.