4 kinds of IO stream Java

4 kinds of IO model java related

1. Synchronous blocking IO

A typical one is the socket TCP example

Server and client communication

Something like this

The following are pseudo codes

// server端 阻塞自己直到收到消息
// 没收到 线程被挂起
msg = server.accept()
// 收到再执行
handler(msg)

2. Synchronous non-blocking IO

// server端 
while(msg=server.accpet() !=null){
	handle(msg)
}

Unlike blocking IO, there must be a return value every time.

3. Multiplexed IO

This should be the most used IO model. Many high-performance IO frameworks are based on multiplexed IO.

Like netty

nio is the basis of multiplexed IO

There are two modes of multiplexing: Proactor mode and Reactor mode

Proactor is used for asynchronous IO

Reactor is used for synchronous IO

  • Reactor mode

    Popular understanding is

    Mother(Client) wantI(Server) noonCooking
    (Event / or IO request),

    But now I want to play games (I do n’t want to wait for the time and then cook)

    Let me tellXiao Ai(Event Register)
    By then Xiao Ai ’sAlarm clock(condition)Remind me to cook(Callbakc call me back)
    Then I know I ’ m goingCooking(Ready to complete IO)

    The event was registered as "I want to cook my own food"

    The result of the callback is "this thing can be done"

  • Proactor mode

    Popular understanding is

    Mother(Client) wantI(Server) noonCooking(Event / or IO request),

    But now I want to play games (I do n’t want to wait for the time and then cook)

    Let me tellXiao AiI want to cook (event separator) probably1 cup of rice and 1.5 cups of water(Data size and buffer area), Xiao Ai students directly start the automaticRice cooker(Operating system) Pour the rice in and add water. The rice cooker is ready, Xiao Ai knows the riceCooked(IO completed).Remind me to eat(Notify me that I can eat)

    Then I got it (finished event)

    The registration of the event is "I want the rice to cook well anyway. I don't care"

    The result of the callback is "This is done according to the requirement of 1.5 cups of water for one cup of rice"

A closer analysis of the Reactor mode generally uses the Reactor mode, because Proactor requires an operating system

There are the following three modes for handling highly concurrent IO requests

The following codes and pictures come from

Doug Lea

Scalable IO in Java

It is recommended to download this pdf to learn

Traditional IO model

Insert picture description here

  1. Single Reactor single processing thread

    One Reactor handles all requests, including io and other operations
    Insert picture description here

  2. Single Reactor multiprocessing thread

    Reactor only completes IO, and the rest is handed over to the worker thread
    Insert picture description here

  3. Multi Reactor Multi Processing Thread

    Separate request and actual operation
    Insert picture description here

4. Asynchronous IO

Rarely used, requires operating system

file = socket.getfile(buff,size);
// 等待一段时间
// buff size 中已经有了file
// 告诉我  我收到了
res = server.finishfile()
Published 22 original articles · Likes2 · Visits 881

Guess you like

Origin blog.csdn.net/weixin_41685373/article/details/104973584