java初入多线程19

基于Socket的服务端多线程模式

public class HeavySocketClient {

    private static ExecutorService  tp =Executors.newCachedThreadPool();
    
    private static final int sleep_time =1000 * 1000 * 1000 ;
    
    public static class EchoClient implements Runnable{
        @Override
        public void run() {
            Socket client =null ;
            PrintWriter writer = null ;
            BufferedReader reader = null ;
            
            try {
                client =new Socket ();
                client.connect(new InetSocketAddress("localhost", 8000));
                writer = new PrintWriter(client.getOutputStream() , true );
                writer.print("H");
                LockSupport.parkNanos(sleep_time);
                writer.println("e");
                LockSupport.parkNanos(sleep_time);
                writer.println("e");
                
                LockSupport.parkNanos(sleep_time);
                writer.println("e");
                
                LockSupport.parkNanos(sleep_time);
                writer.println("e");
                
                LockSupport.parkNanos(sleep_time);
                writer.println("e");
                
                LockSupport.parkNanos(sleep_time);
                writer.println("e");
                writer.flush();
                
                reader =new BufferedReader(new InputStreamReader(client.getInputStream()));
                System.out.println("from server : " +reader.readLine());
                
            } catch (Exception e) {
                e.printStackTrace();
            }       
        }
    }   
}
public class MultiThreadEchoServer {

    private static ExecutorService tp =Executors.newCachedThreadPool();
    
    static class HandleMsg implements Runnable{

        Socket clienSocket ;
        
        
        public HandleMsg(Socket clienSocket) {
            super();
            this.clienSocket = clienSocket;
        }


        @Override
        public void run() {
            BufferedReader is = null ;
            PrintWriter os = null ;
            try {
                is = new BufferedReader( new InputStreamReader(clienSocket.getInputStream()));
                os =new PrintWriter(clienSocket.getOutputStream(),true);
                
                String inputLine = null;
                long b =System.currentTimeMillis() ;
                
                while ((inputLine = is.readLine()) != null){
                    os.println(inputLine);
                }
                long e =System.currentTimeMillis() ;
                System.out.println("spend:"+ (e - b) + "ms");
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if(is != null){
                        is.close();
                    }
                    if(os != null){
                        os.close();
                    }
                    clienSocket.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
    
    public static void main(String[] args) {
        ServerSocket echoServer = null ;
        Socket clientSocket = null ;
        try {
            echoServer =new ServerSocket(8000);
        } catch (Exception e) {
            System.out.println(e);
        }
        
        while(true){
            try {
                clientSocket = echoServer.accept() ;
                System.out.println(clientSocket.getRemoteSocketAddress() + " connect !");
                tp.execute(new HandleMsg(clientSocket));
                
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
    
}
  • 客户端与服务端的代码实现 ,在客户端,我们使用线程池实现10次请求,因为会在输出有个字符之后。进行1秒的等待。所以会延迟很长时间。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,322评论 19 139
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,429评论 11 349
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,899评论 18 399
  • 想起自动放弃跳舞表演的女儿,我笑了笑问女孩:“你不喜欢表演是不是?”小孩终于点了点头。家长后来跟我说:她女儿学钢琴...
    嫣姐妹阅读 3,150评论 0 1
  • 从现在开始,确定自己未来一万小时的努力方向并坚持下去,那么在下一个十年之后,你也将成为“世界级大师”! 这段话是《...
    晶晶亮的沙子阅读 3,290评论 1 4