Java Reference
In-Depth Information
9
10
public PoolDispatcher() {
11
// Get the number of threads from the System properties or take the default
12
numThreads = Integer.parseInt(System.getProperty(THREADPROP, NUMTHREADS));
13
}
14
15
public void startDispatching(final ServerSocket servSock, final Logger logger,
16
final ProtocolFactory protoFactory) {
17
// Create Nāˆ’1 threads, each running an iterative server
18
for(inti=0;i<(numThreads āˆ’ 1); i++) {
19
Thread thread = new Thread() {
20
public void run() {
21
dispatchLoop(servSock, logger, protoFactory);
22
}
23
};
24
thread.start();
25
logger.writeEntry("Created and started Thread="+thread.getName());
26
}
27
logger.writeEntry("Iterative server starting in main thread " +
28
Thread.currentThread().getName());
29
// Use main thread as Nth iterative server
30
dispatchLoop(servSock, logger, protoFactory);
31
/* NOT REACHED */
32
}
33
34
private void dispatchLoop(ServerSocket servSock, Logger logger,
35
ProtocolFactory protoFactory) {
36
// Run forever, accepting and handling each connection
37
for (;;) {
38
try {
39
Socket clntSock = servSock.accept(); // Block waiting for connection
40
Runnable protocol = protoFactory.createProtocol(clntSock, logger);
41
protocol.run();
42
} catch (IOException e) {
43
logger.writeEntry("Exception="+ e.getMessage());
44
}
45
}
46
}
47 }
PoolDispatcher.java
1. PoolDispatcher() : lines 10-13
The thread-pool solution needs an additional piece of information: the number of threads
in the pool. We need to provide this information to the instance before the thread pool is
constructed. We could pass the number of threads to the constructor, but this limits our
options because the constructor interface varies by dispatcher. We use system properties
Search WWH ::




Custom Search