zoukankan      html  css  js  c++  java
  • 多线程分析

    package com.batman.eureka;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class HClient {
        static int i = 0;
        public static void main(String[] args) {
            while (true) {
                try {
                    Socket socket = new Socket("127.0.0.1", 1234);
                   /* System.out.println("please input...");
                    Scanner scanner = new Scanner(System.in);
                    String p = scanner.nextLine();
                    if (p.equals("bye")) {
                        socket.close();
                        break;
                    }*/
                    String p = "my client";
                    i = i + 1;
                    if(i == 51)
                        break;
                    // 发送给服务器的数据
                    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                    out.writeUTF(p + "  " + i);
                    // 接收服务器的返回数据
                  /*  DataInputStream in = new DataInputStream(socket.getInputStream());
                    System.out.println("hserver:" + in.readUTF());*/
                    socket.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    package com.batman.eureka;
    
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class HServerApp implements Runnable {
        public int port;
        
      //  ExecutorService executorService = Executors.newCachedThreadPool();
        ExecutorService executorService = Executors.newFixedThreadPool(5);  
        public HServerApp(int port) {
            this.port = port;
        }
    
        @Override
        public void run() {
            try {
                ServerSocket server = new ServerSocket(port);
                while (true) {
                    //等待client的请求
                   // System.out.println("waiting...");
                    Socket socket = server.accept();
                    // 接收客户端的数据
                    DataInputStream in = new DataInputStream(socket.getInputStream());
                    String str = in.readUTF();
                    //socket.close();
                   /* try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }*/
                    System.out.println("client:" + str);
                    executorService.execute(new TestRunnable()); 
                    // 发送给客户端数据
                   /* DataOutputStream out = new DataOutputStream(socket.getOutputStream());
                    out.writeUTF("hi,i am hserver!i say:" + str);*/
                    
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) {
            HServerApp serverApp = new HServerApp(1234);
            serverApp.run();
        }
    }
    
    class TestRunnable implements Runnable{   
        public void run(){  
            System.out.println(Thread.currentThread().getName() + "线程被调用了。");   
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }   
    }  
  • 相关阅读:
    [转]Java中fina以及static的意义
    [转]Java中this的意义
    [转]Java中子类调用父类构造方法的问题分析
    [原创]SSH中HibernateTemplate与HibernateDaoSupport关系
    [转]No configuration found for the specified action解决办法
    [原创]MyEclipse2014全手动实现反向工程---解决手动整合ssh时发生的、在hibernate反向工程的时候找不到项目名的问题
    [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
    pycharm中运行时添加配置 及pytest模式怎么修改为run模式
    字符串正则匹配替换
    PyCharm选中文件夹新建时Directory与Python package的区别
  • 原文地址:https://www.cnblogs.com/mutong1228/p/9226905.html
Copyright © 2011-2022 走看看