34 |
|
* <p>The {@link Executors} class provides factory methods for the |
35 |
|
* executor services provided in this package. |
36 |
|
* |
37 |
+ |
* <h3>Usage Example</h3> |
38 |
+ |
* |
39 |
+ |
* Here is a sketch of a network service in which threads in a thread |
40 |
+ |
* pool service incoming requests. It uses the preconfigured {@link |
41 |
+ |
* Executors#newFixedThreadPool} factory method: |
42 |
+ |
* |
43 |
+ |
* <pre> |
44 |
+ |
* class NetworkService { |
45 |
+ |
* private final ServerSocket serverSocket; |
46 |
+ |
* private final ExecutorService pool; |
47 |
+ |
* |
48 |
+ |
* public NetworkService(int port, int poolSize) throws IOException { |
49 |
+ |
* serverSocket = new ServerSocket(port); |
50 |
+ |
* pool = Executors.newFixedThreadPool(poolSize); |
51 |
+ |
* } |
52 |
+ |
* |
53 |
+ |
* public void serve() { |
54 |
+ |
* try { |
55 |
+ |
* for (;;) { |
56 |
+ |
* pool.execute(new Handler(serverSocket.accept())); |
57 |
+ |
* } |
58 |
+ |
* } catch (IOException ex) { |
59 |
+ |
* pool.shutdown(); |
60 |
+ |
* } |
61 |
+ |
* } |
62 |
+ |
* } |
63 |
+ |
* |
64 |
+ |
* class Handler implements Runnable { |
65 |
+ |
* private final Socket socket; |
66 |
+ |
* Handler(Socket socket) { this.socket = socket; } |
67 |
+ |
* public void run() { |
68 |
+ |
* // read and service request |
69 |
+ |
* } |
70 |
+ |
* } |
71 |
+ |
* </pre> |
72 |
|
* @since 1.5 |
73 |
|
* @author Doug Lea |
74 |
|
*/ |