-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleNIOEchoServerMT.java
43 lines (37 loc) · 1.67 KB
/
SimpleNIOEchoServerMT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package co.iay.learn.learningjava.nio;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SimpleNIOEchoServerMT {
public static void main(String[] args) {
String hostname = System.getProperty("hostname", "127.0.0.1");
String port = System.getProperty("port", "12345");
String backlog = System.getProperty("backlog", "10000");
String ioThreads = System.getProperty("ioThreads", "1");
ExecutorService acceptThreadPool = Executors.newCachedThreadPool();
ExecutorService ioThreadPool = Executors.newCachedThreadPool();
String[] ports = port.split(",");
SimpleNIOEchoServerAcceptHandler[] acceptHandlers = new SimpleNIOEchoServerAcceptHandler[ports.length];
SimpleNIOEchoServerIOHandler[] ioHandlers = new SimpleNIOEchoServerIOHandler[Integer.parseInt(ioThreads)];
try {
for (int i = 0; i < ioHandlers.length; ++i) {
ioHandlers[i] = new SimpleNIOEchoServerIOHandler();
ioThreadPool.submit(ioHandlers[i]);
}
for (int i = 0; i < acceptHandlers.length; ++i) {
acceptHandlers[i] = new SimpleNIOEchoServerAcceptHandler(hostname, Integer.parseInt(ports[i]), Integer.parseInt(backlog), ioHandlers, i + 1, ioHandlers.length);
acceptThreadPool.submit(acceptHandlers[i]);
}
} catch (Exception e) {
e.printStackTrace();
return;
}
while (true) {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}