在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 –