在a simple test case我已經實現一個線程池服務器在端口12345接收多達10個同時傳入TLS PSK連接和印刷在標準輸出解密後的數據:如何預先分配線程池服務器的對象?
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));
}
}
這裏是由螺紋使用的Runnable
實現:
@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);
}
}
如何預分配由Runnable
使用的SecureRandom
和MockPSKTlsServer
對象?
I.e.如何在main()
中創建兩個對象中的10個,然後在run()
中重新使用它們?
你使用哪個版本的Java? –
我使用Windows和Linux –