博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bio服务端两个实例
阅读量:7173 次
发布时间:2019-06-29

本文共 2903 字,大约阅读时间需要 9 分钟。

hot3.png

对比Nio时顺带产物,Bio有明显的劣势

package bio;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import java.util.logging.Level;import java.util.logging.Logger;public class BioTimeServer {    public static void main(String[] args) throws Exception {        oneToOneThread();  // 启动线程一对一服务模型        threadPool();      // 启动线程池模型    }        /**     * 线程池模式,限制了并发线程数和最大池连接数,资源可控不会宕机,但也没有摆脱并发性能问题     * @throws Exception      */    public static void threadPool() throws Exception {        ServerSocket server = new ServerSocket(9998);        TimeServerHandlerExecutePool singleExecutor = new TimeServerHandlerExecutePool(50, 10000);        while (true) {            System.out.println("Waiting for client... ");            Socket socket = server.accept();            singleExecutor.execute(new Thread(new ProcThread(socket)));        }    }        /**     * 普通接收方式,一个线程对应一个客户端连接,但资源不可控,数千并发甚至可能导致宕机     * @throws Exception      */    public static void oneToOneThread() throws Exception {        ServerSocket server = new ServerSocket(9999);        while (true) {            System.out.println("Waiting for client... ");            Socket socket  = server.accept();            new Thread(new ProcThread(socket)).start();        }    }}class TimeServerHandlerExecutePool {    private ExecutorService executor;        public TimeServerHandlerExecutePool(int maxPoolSize, int queueSize) {        executor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), maxPoolSize, 120L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(queueSize));    }        public void execute(Runnable task) {        executor.execute(task);    }}class ProcThread implements Runnable {        private Socket conn;        public ProcThread(Socket conn) {        this.conn = conn;    }    @Override    public void run() {        InputStream is;        OutputStream os;        try {            is = conn.getInputStream();            os = conn.getOutputStream();                        int bytesRead = 0;            byte[] buffer = new byte[8192];            while ((bytesRead = is.read(buffer)) != -1) {                System.out.println(new String(buffer, 0, bytesRead));                os.write("copy that.\r\n".getBytes());                os.flush();            }        } catch (IOException ex) {            Logger.getLogger(ProcThread.class.getName()).log(Level.SEVERE, null, ex);        } finally {            try {                conn.close();            } catch (IOException ex) {                Logger.getLogger(ProcThread.class.getName()).log(Level.SEVERE, null, ex);            }            conn = null;        }    }}

 

转载于:https://my.oschina.net/u/2953734/blog/1543087

你可能感兴趣的文章
我的友情链接
查看>>
安装虚拟机shell脚本
查看>>
[Python]第一个爬虫练习
查看>>
rpm,yum,权限
查看>>
tomcat应用转到weblogic上时的问题
查看>>
vSphere 5.5 vCenter迁移至分布式交换机
查看>>
第二次作业
查看>>
viewport ——视区概念
查看>>
拓扑规则翻译函数(转)
查看>>
数据结构--图的定义和存储结构
查看>>
linux常用命令
查看>>
Appium自动化测试1 - 安装部署
查看>>
Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
查看>>
UVa294 Divisors
查看>>
洛谷P3406 海底高铁
查看>>
1、JUC--volatile 关键字-内存可见性
查看>>
uboot arp地址解析
查看>>
Java字节码 小结
查看>>
sed 替换多个空格为一个
查看>>
ControlTemplate in WPF —— DataGrid
查看>>