【lucene系列学习三】用socke把lucene做成一个web服务并实现多线程
首先,参考http://www.cnblogs.com/itlqs/p/6104672.html和http://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(); } }
改造为
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(); } }
就建立了多客户并行的Socket