Block IO comprehension

Source: Internet
Author: User

Generally blocking IO server communication, there is usually a separate acceptor thread responsible for monitoring client contact, which receives the client for each request connected after the client is assigned to process a new thread after processing. Returns the reply to the client. Line friend destroyed.

Take a look at the code that plugs the Ioserver:

Server Startup class

Package Com.bio.demo.server;import Java.io.ioexception;import Java.net.serversocket;import java.net.Socket;import com.bio.demo.server.handler.timerserverhandler;/** * @author Zhouxuejun * * @date October 20, 2014 PM 7:08:58 */public Class Ti Meserver {public     static serversocket server=null;/** * @param args */public static void main (string[] args) {//TODO Auto-generated method Stubtry {server=new serversocket (8080); Socket Socket=null;while (True) {socket=server.accept (); New Thread (New Timerserverhandler (socket)). Start ();}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}       

Processing Thread Classes:

Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.printwriter;import java.net.socket;/** * @author Zhouxuejun * * @date October 20, 2014 afternoon 7:17:28 */public class Timerse  Rverhandler implements Runnable {private socket socket; public timerserverhandler (socket socket) {//TODO auto-generated constructor stub This.socket=socket;} /* (non-javadoc) * @see java.lang.runnable#run () */@Overridepublic void Run () {//TODO auto-generated method stub Buf      Feredreader In=null;      PrintWriter Out=null; try {in=new BufferedReader (new InputStreamReader (This.socket.getInputStream ())); Out=new PrintWriter ( This.socket.getOutputStream ()); String Body=null;        String Tag=null;while (True) {body=in.readline (); if (null==body) break; Out.print (body+ "_return");  }} catch (IOException e) {//TODO auto-generated catch Blockif (null!=in) {try {in.close ();} catch (IOException E1) {//TODO Auto-generated catch Blocke1.printstacktrace ();}} if (null!=out) {Out.close(); out=null;} if (Null!=this.socket) {try {this.socket.close ()} catch (IOException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} This.socket=null;} E.printstacktrace ();}}}

Can be seen through the code above. Whenever a new client request comes in, the server needs to create a new thread to handle the new incoming client request, and a thread can only process a client request.

In high-performance server applications, concurrent access is often required for thousands of clients. Blocking IO clearly does not meet high performance, highly concurrent field access.



Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Block IO comprehension

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.