The problem is obvious: each client will need to create a thread, concurrent access to large, stack overflow will occur, and other issues failed to create a new thread
publicstaticvoid(String[] args)throws IOException { int port = 8080; if (args != null && args.length > 0) { port = Integer.valueOf(args[0]); }
ServerSocket server = null; try { server = new ServerSocket(port); System.out.println("The time server is start in port : " + port); Socket socket; while (true) { socket = server.accept(); new Thread(new TimeServerHandler(socket)).start(); } } finally { if (server != null) { System.out.println("The time server close"); server.close(); } } } }
publicstaticvoidmain(String[] args){ int port = 8080; if (args != null && args.length > 0) { port = Integer.valueOf(args[0]); }
Socket socket = null; BufferedReader in = null; PrintWriter out = null;
try { socket = new Socket("127.0.0.1", port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true);
out.println("QUERY TIME ORDER"); System.out.println("Send order 2 server succeed.");
You can see, Pseudo asynchronous I / O just joined a thread pool to avoid the problems created for every request thread (here is not to write a demo)
However, by API documentation of the analysis of input and output streams, read and write operations are synchronized learned blocked, blocking the transmission time depends on the speed of the other I / O thread processing speed and network I / O's. We can not guarantee that the network conditions of the production environment and the application process can be fast enough to end, so its reliability is very poor.