In a simple test case I have implemented a thread pooled server accepting up to 10 simultaneous incoming TLS PSK connections at port 12345 and printing decrypted data at standard output:
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(12345);
ExecutorService pool = Executors.newFixedThreadPool(10);
while (true) {
Socket socket = server.accept();
pool.execute(new MyRunnable(socket));
}
}
Here is the Runnable implementation used by the threads:
@Override
public void run() {
try {
SecureRandom random = new SecureRandom(); // How to preallocate?
BufferedInputStream bis = new BufferedInputStream(mSocket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(mSocket.getOutputStream());
TlsServerProtocol proto = new TlsServerProtocol(bis, bos, random);
MockPSKTlsServer server = new MockPSKTlsServer(); // How to preallocate?
proto.accept(server);
Streams.pipeAll(proto.getInputStream(), System.out);
proto.close();
} catch (IOException e) {
System.err.print(e);
}
}
How to preallocate the SecureRandom and MockPSKTlsServer objects used by the Runnable?
I.e. how to create 10 of both objects in the main() and then just reuse them in the run()?
In your case I would use a
ThreadLocalfor each class (SecureRandomandMockPSKTlsServer), to have one dedicated instance ofSecureRandomandMockPSKTlsServerfor each thread of your connection pool and reuse them when the threads will have to execute the same type of task but with different inputSocket, something like:NB: Use the
try-with-resourcesstatement to automatically close your input/output streams.