Share the joy
Look at this code, it won’t stop:
public static void main(String[] args) throws Exception{ ExecutorService executorService = Executors.newSingleThreadExecutor(); Runnable r = () -> { System.out.println(1); }; executorService.submit(r); }
Main reason is that it didn’t call executorService.shutdown();
Exector is a simple interface. It only has a execute() function. ExecutorService has shutdown() function, which can stop the thread.
So below is the complete one:
public static void main(String[] args) throws Exception{ ExecutorService executorService = Executors.newSingleThreadExecutor(); Runnable r = () -> { System.out.println(1); }; executorService.submit(r); executorService.shutdown(); }
Pingback: Create ExecutorService and ListeningExecutorService | allenlipeng47()