Biblioteca Java - Blame information for rev 28
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
28 | mihai | 1 | /* |
2 | * NetworkService.java | ||
3 | */ | ||
4 | package exemple.fire.tpool2; | ||
5 | |||
6 | import java.io.IOException; | ||
7 | import java.net.ServerSocket; | ||
8 | import java.net.Socket; | ||
9 | import java.util.concurrent.ExecutorService; | ||
10 | import java.util.concurrent.Executors; | ||
11 | |||
12 | /** | ||
13 | * Class created by @author Mihai HULEA at Feb 22, 2005. | ||
14 | * | ||
15 | * This class is part of the labs project. | ||
16 | * | ||
17 | * Ecesta este un exemplu preluat din documentatia sdk 1.5.0 care exemplifica modul | ||
18 | * de folosire a unui threadpool pentru crearea unui server ce deserveste clienti pe | ||
19 | * un numar limitat de fire. | ||
20 | * | ||
21 | */ | ||
22 | class NetworkService { | ||
23 | private final ServerSocket serverSocket; | ||
24 | private final ExecutorService pool; | ||
25 | |||
26 | public NetworkService(int port, int poolSize) throws IOException { | ||
27 | serverSocket = new ServerSocket(port); | ||
28 | pool = Executors.newFixedThreadPool(poolSize); | ||
29 | } | ||
30 | |||
31 | public void serve() { | ||
32 | try { | ||
33 | for (;;) { | ||
34 | pool.execute(new Handler(serverSocket.accept())); | ||
35 | } | ||
36 | } catch (IOException ex) { | ||
37 | pool.shutdown(); | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * Clasa implementeaza protocolul de comunicatie cu clientul. Pentru fiecare client | ||
44 | * va fi lansat in executie un fir de tip Handler. | ||
45 | */ | ||
46 | class Handler implements Runnable { | ||
47 | private final Socket socket; | ||
48 | Handler(Socket socket) { this.socket = socket; } | ||
49 | public void run() { | ||
50 | // read and service request | ||
51 | } | ||
52 | } |