OnJava8-Examples/concurrent/SingleThreadExecutor3.java
2016-11-23 09:05:26 -08:00

30 lines
807 B
Java

// concurrent/SingleThreadExecutor3.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import java.util.concurrent.*;
public class SingleThreadExecutor3 {
public static void main(String[] args)
throws InterruptedException {
ExecutorService exec =
Executors.newSingleThreadExecutor();
for(int id = 0; id < 10; id++)
exec.execute(new InterferingTask(id));
exec.shutdown();
exec.awaitTermination(5, TimeUnit.SECONDS);
}
}
/* Output:
0 pool-1-thread-1 100
1 pool-1-thread-1 200
2 pool-1-thread-1 300
3 pool-1-thread-1 400
4 pool-1-thread-1 500
5 pool-1-thread-1 600
6 pool-1-thread-1 700
7 pool-1-thread-1 800
8 pool-1-thread-1 900
9 pool-1-thread-1 1000
*/