古之成大事者,不唯有超世之才,亦唯有坚韧不拔之志也!

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  3 随笔 :: 44 文章 :: 1 评论 :: 0 Trackbacks
线程组式模
    每客户一线程的模式增加了服务端的负荷,那么我们可以简单的重用服务端处理线程。
    服务端重用一定的线程来处理客户端请求,客户端请求被放置到一个池中供并发线程取出处理
package com.lei.socket.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;


public class PooledClientServer {

    
    
//启动服务端监听
    public void start(){
        
try{
            setUpHandlers();
            ServerSocket server 
= new ServerSocket(port);
            Socket client 
= null;
            
while(runFlag){
                client 
= server.accept();
                System.out.println(
"accept client " + client.getInetAddress().getHostAddress());
                ClientHandle.handleClient(client);
            }
            server.close();
        }
catch(IOException e){
            System.out.println(
"start error");
        }
    }
    
    
//创建处理线程组
    public void setUpHandlers() {
        
for (int i = 0; i < threadNum; i++) {
            ClientHandle ch 
= new ClientHandle();
            
new Thread(ch, "Handler " + i).start();
        }
    }
    
    
public static void main(String[] args){
        PooledClientServer s 
= new PooledClientServer();
        s.start();
    }
    
    
    
private int port = 3000;
    
    
//并发处理线程数
    private int threadNum = 3;
    
    
private boolean runFlag = true;
}



//处理类
class ClientHandle implements Runnable{

    
public static void handleClient(Socket client) {
        
synchronized (pool) {
            pool.add(client);
            pool.notifyAll();
        }
    }
    
    
//处理客户端
    private void handleClient(){
        
try{
            BufferedReader inReader 
= new BufferedReader(new InputStreamReader(client.getInputStream()));
            BufferedWriter outWriter 
= new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
            
while(true){
                outWriter.write(
"please input String->");
                outWriter.flush();
                String str 
= inReader.readLine();
                
if(str.equalsIgnoreCase("quit")){
                    outWriter.write(
"quit ok");
                    client.close();
                    
break;
                }
                
else{
                    outWriter.write(
"reulst is :" + new StringBuffer(str).reverse().toString());
                    outWriter.write(
"\r\n");
                }
            }
        }
catch(IOException e){
            System.out.println(
"Handle client error");
        }
    }
    
    
public void run(){
        
while (true) {
            
synchronized (pool) {
                
while (pool.isEmpty()) {
                    
try {
                        pool.wait();
                    } 
catch (InterruptedException e) {
                        e.printStackTrace();
                        
return;
                    }
                }
                client 
= (Socket) pool.remove(0);
            }
            handleClient();
        }
    }
    
    
private Socket client;
    
private static ArrayList pool = new ArrayList();
}


posted on 2008-09-19 15:35 goto 阅读(199) 评论(0)  编辑  收藏 所属分类: SOCKET

只有注册用户登录后才能发表评论。


网站导航: