Java BIO pseudo programming and asynchronous I / O programming

Excerpt from "Netty Definitive Guide"

BIO communication model:

BIO communication model

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

Code demonstrates

Function: Time server

Server:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public  class   {

public static void (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();
}
}
}
}



*/
public class TimeServerHandler implements Runnable{

private Socket socket;

public TimeServerHandler(Socket socket) {
this.socket = socket;
}


public void run() {
BufferedReader in = null;
PrintWriter out = null;

try {
in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
out = new PrintWriter(this.socket.getOutputStream(), true);

String currentTime;
String body;

while (true) {
body = in.readLine();
if (body == null) {
break;
}
System.out.println("The time server receive order : " + body);
currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)
? new Date(Sy 大专栏  Java BIO编程和伪异步I/O编程stem.currentTimeMillis()).toString() : "BAD ORDER";
out.println(currentTime);
}
} catch (Exception e) {
e.printStackTrace ();
} the finally { // release resources associated IF (in =! null ) { the try { in.close (); } the catch (IOException E) { e.printStackTrace (); } }









if (out != null) {
out.close();
}

if (this.socket != null) {
try {
this.socket.close();
} catch (IOException e) {
e.printStackTrace();
}
this.socket = null;
}
}
}
}

Client:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class TimeClient {

public static void main(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.");

String resp = in.readLine();
System.out.println("Now is : " + resp);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放相关资源
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}

if (out != null) {
out.close();
}

if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

Pseudo asynchronous I / O

Pseudo asynchronous I / O model diagram:

Pseudo Asynchronous IO model

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.

Guess you like

Origin www.cnblogs.com/liuzhongrong/p/11986021.html