欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

IO的4种方式 博客分类: nio java network  

程序员文章站 2024-03-17 12:17:34
...

Blocking IO

{

byte[] buffer = ...

read(socket, buffer); // blocking

process(buffer);

}

 

Non blocking (Sync) IO 

{

byte[] buffer = ...

while ( read(socket, buffer) != 'succeed' ) ; // non blocking but sync

process (buffer);

}

 

Multiplexing blocking IO (blocking, sync or async?, to the socket processing thread, it's sync; to the invoking client, it's async)

{

register(socket);

while (true) {

    sockets = select();// blocking until data is ready for read

    for (s : sockets) {

         if (can_read(s) ) {

            // data is ready, will not be blocked

            read(s, buffer);

            process(buffer);

         }

    }

}

}

 

4. Async Non blocking

{

    register(socket, handleBuffer);

}

 

handleBuffer(buffer) {

    // process

}