(String[] args)
| 8 | |
| 9 | public class SingleThreadExecutor { |
| 10 | public static void main(String[] args) { |
| 11 | ExecutorService exec = |
| 12 | Executors.newSingleThreadExecutor(); |
| 13 | IntStream.range(0, 10) |
| 14 | .mapToObj(NapTask::new) |
| 15 | .forEach(exec::execute); |
| 16 | System.out.println("All tasks submitted"); |
| 17 | exec.shutdown(); |
| 18 | while(!exec.isTerminated()) { |
| 19 | System.out.println( |
| 20 | Thread.currentThread().getName() + |
| 21 | " awaiting termination"); |
| 22 | new Nap(0.1); |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | /* Output: |
| 27 | All tasks submitted |