• 【lucene系列学习三】用socke把lucene做成一个web服务并实现多线程


    首先,参考http://www.cnblogs.com/itlqs/p/6104672.htmlhttp://www.cnblogs.com/itlqs/p/6079301.html

    然后,LocalSocketServer类继承Thread类

    把单线程的实现代码

        public static void main(String[] args) throws IOException
        {
            ServerSocket server=new ServerSocket(5678);
            while (true)
            {
                Socket client=server.accept();
                BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
                PrintWriter out=new PrintWriter(client.getOutputStream());
                String str=in.readLine();
                System.out.println(str);
                out.println(indexSearch(str));
                out.flush();
                client.close();
            }
        }
    View Code

    改造为

    private  Socket client;
        public LocalSocketServer(Socket c)
        {
            this.client=c;
        }
        public void run()
        {
            try{
                BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
                PrintWriter out=new PrintWriter(client.getOutputStream());
                String str=in.readLine();
                System.out.println(str);
                out.println(indexSearch(str));
                out.flush();
                client.close();
            }catch(IOException ex){
                    
                }finally{
                    
                }
            }
        public static void main(String[] args) throws IOException
        {
            ServerSocket server=new ServerSocket(5678);
            while (true)
            {
                LocalSocketServer lss=new LocalSocketServer(server.accept());
                lss.start();
            }
        }
    View Code

    就建立了多客户并行的Socket

  • 相关阅读:
    按钮字体颜色的设置
    异常
    数据存储
    SQLiteOpenHelper
    MVC
    在单线程模型中 Message、Handler、Message Queue、Looper 之间的关系
    ListView 的优化方案
    fragment生命周期及优点
    ANR
    Android系统架构
  • 原文地址:https://www.cnblogs.com/itcsl/p/6490593.html
Copyright © 2020-2023  润新知