面试刷题11:java系统中io的分类有哪些?
程序员文章站
2022-09-04 22:42:59
随着分布式技术的普及和海量数据的增长,io的能力越来越重要,java提供的io模块提供了足够的扩展性来适应。 我是李福春,我在准备面试,今天的问题是: java中的io有哪几种? java中的io分3类: 1,BIO ,即同步阻塞IO,对应java.io包提供的工具;基于流模型,虽然直观,代码实现也 ......
随着分布式技术的普及和海量数据的增长,io的能力越来越重要,java提供的io模块提供了足够的扩展性来适应。
我是李福春,我在准备面试,今天的问题是:
java中的io有哪几种?
java中的io分3类:
1,bio ,即同步阻塞io,对应java.io包提供的工具;基于流模型,虽然直观,代码实现也简单,但是扩展性差,消耗资源大,容易成为系统的瓶颈;
2,nio,同步非阻塞io,对应java.nio包提供的工具,基于io多路复用;
核心类: channel ,selector , buffer , charset
selector是io多路复用的基础,实现了一个线程高效管理多个客户端连接,通过事件监听处理感兴趣的事件。
3,aio,即异步非阻塞io, 基于事件和回调
io的类层级
java各种io的例子
java.io客户端连接服务端例子
package org.example.mianshi.io; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.inetaddress; import java.net.serversocket; import java.net.socket; /** * 说明:传统流式io 客户端连接服务端例子 * @author carter * 创建时间: 2020年03月25日 9:58 下午 **/ public class javaioapp { public static void main(string[] args) { final server server = new server(); new thread(server).start(); try ( socket socket = new socket(inetaddress.getlocalhost(), server.getport()); ) { final bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(socket.getinputstream())); bufferedreader.lines().foreach(system.out::println); } catch (ioexception e) { e.printstacktrace(); } } public static class server implements runnable { private serversocket serversocket; public int getport() { return serversocket.getlocalport(); } @override public void run() { try (serversocket serversocket = new serversocket(0);) { this.serversocket = serversocket; while (true) { final socket socket = serversocket.accept(); new requesthandler(socket).start(); } } catch (ioexception e) { e.printstacktrace(); } } private class requesthandler extends thread { private socket socket; public requesthandler(socket socket) { this.socket = socket; } @override public void run() { try ( final printwriter printwriter = new printwriter(socket.getoutputstream()); ) { printwriter.write("hello world"); printwriter.flush(); } catch (ioexception e) { e.printstacktrace(); } } } } }
使用连接池优化
package org.example.mianshi.io; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.inetaddress; import java.net.serversocket; import java.net.socket; import java.util.concurrent.executorservice; import java.util.concurrent.executors; /** * 说明:传统流式io 客户端连接服务端例子 * @author carter * 创建时间: 2020年03月25日 9:58 下午 **/ public class threadpooljavaioapp { public static void main(string[] args) { final server server = new server(); new thread(server).start(); try ( socket socket = new socket(inetaddress.getlocalhost(), server.getport()); ) { final bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(socket.getinputstream())); bufferedreader.lines().foreach(system.out::println); } catch (ioexception e) { e.printstacktrace(); } } public static class server implements runnable { private executorservice threadpool = executors.newfixedthreadpool(4); private serversocket serversocket; public int getport() { return serversocket.getlocalport(); } @override public void run() { try (serversocket serversocket = new serversocket(0);) { this.serversocket = serversocket; while (true) { final socket socket = serversocket.accept(); threadpool.submit(new requesthandler(socket)); } } catch (ioexception e) { e.printstacktrace(); }finally { threadpool.shutdown(); } } private class requesthandler implements runnable { private socket socket; public requesthandler(socket socket) { this.socket = socket; } @override public void run() { try ( final printwriter printwriter = new printwriter(socket.getoutputstream()); ) { printwriter.write("hello world"); printwriter.flush(); } catch (ioexception e) { e.printstacktrace(); } } } } }
java.nio例子
package org.example.mianshi.io; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.inetaddress; import java.net.inetsocketaddress; import java.net.socket; import java.nio.channels.selectionkey; import java.nio.channels.selector; import java.nio.channels.serversocketchannel; import java.nio.channels.socketchannel; import java.nio.charset.charset; /** * 说明:nio的客户端连接服务端例子 * @author carter * 创建时间: 2020年03月25日 10:32 下午 **/ public class javanioapp { public static void main(string[] args) { new server().start(); try ( socket socket = new socket(inetaddress.getlocalhost(), 8888); ) { final bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(socket.getinputstream())); bufferedreader.lines().foreach(system.out::println); } catch (ioexception e) { e.printstacktrace(); } } public static class server extends thread { @override public void run() { try { serversocketchannel serversocketchannel = serversocketchannel.open(); serversocketchannel.bind(new inetsocketaddress(inetaddress.getlocalhost(), 8888)); serversocketchannel.configureblocking(false); final selector selector = selector.open(); serversocketchannel.register(selector, selectionkey.op_accept); while (true) { selector.select(); selector.selectedkeys().foreach(selectionkey -> { sayhelloworld((serversocketchannel) selectionkey.channel()); }); } } catch (ioexception e) { e.printstacktrace(); } } private void sayhelloworld(serversocketchannel channel) { try (socketchannel socketchannel = channel.accept()) { socketchannel.write(charset.defaultcharset().encode("hello world nio")); } catch (ioexception e) { e.printstacktrace(); } } } }
![image.png](https://img2020.cnblogs.com/other/268922/202003/268922-20200325233150738-94393984.png)
java.nio2例子
package org.example.mianshi.io; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.inetaddress; import java.net.inetsocketaddress; import java.net.socket; import java.nio.channels.asynchronousserversocketchannel; import java.nio.channels.asynchronoussocketchannel; import java.nio.channels.completionhandler; import java.nio.charset.charset; /** * 说明:todo * @author carter * 创建时间: 2020年03月25日 10:54 下午 **/ public class javanio2app { public static void main(string[] args) { new server().start(); try ( socket socket = new socket(inetaddress.getlocalhost(), 9999); ) { final bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(socket.getinputstream())); bufferedreader.lines().foreach(system.out::println); } catch (ioexception e) { e.printstacktrace(); } } public static class server extends thread { @override public void run() { try { asynchronousserversocketchannel serversocketchannel = asynchronousserversocketchannel.open() .bind(new inetsocketaddress(inetaddress.getlocalhost(), 9999)); serversocketchannel.accept(serversocketchannel, new completionhandler<asynchronoussocketchannel, asynchronousserversocketchannel>() { @override public void completed(asynchronoussocketchannel socketchannel, asynchronousserversocketchannel serversocketchannel1) { // serversocketchannel1.accept(socketchannel, this); socketchannel.write(charset.defaultcharset().encode("hello world nio2 ")); try { socketchannel.close(); } catch (ioexception e) { e.printstacktrace(); } } @override public void failed(throwable exc, asynchronousserversocketchannel attachment) { exc.printstacktrace(); } }); } catch (ioexception e) { e.printstacktrace(); } } } }
本例子暂时无法运行。只为展示过程;
小结
本篇主要介绍了java提供的3中io,即 bio,nio,aio ; 并提供了一些示例代码辅助理解。
原创不易,转载请注明出处。
下一篇: 姿势-1