客户端连接到服务器,每连一个客户端则起一个线程,现在想把客户端的线程放在队列里,使用队列管理这些客户端线程,求思路,最好有个demo,谢谢。我的客户端、服务器端的代码如下:
客户端:public class ChatClient extends JFrame{
/**
 * 
 */
private static final long serialVersionUID = -5823809678314632583L;
JTextField tfTxt=new JTextField();
JTextArea taContent=new JTextArea();
Socket s=null;
DataOutputStream dos=null;
DataInputStream dis=null;
private boolean bConnected=false;
private Thread thread=new Thread(new ReceThread() );
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
        new ChatClient().launchFrame();
}
  

public void launchFrame(){
setLocation(400,300);
this.setSize(300,300);
add(tfTxt,BorderLayout.SOUTH);
add(taContent,BorderLayout.NORTH);
//pack();
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
disconnect();
System.exit(0);
}


});
tfTxt.addActionListener(new TFListener());

setVisible(true);
connect();
thread.start();
}

//connect to server
public void connect(){
try {
 s=new Socket("10.2.135.42",8800);
 bConnected=true;
 dos=new DataOutputStream(s.getOutputStream());
             dis=new DataInputStream(s.getInputStream());
//System.out.println("connected!");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void disconnect(){

try {
dos.close();
dis.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

//内部类
private class TFListener implements ActionListener{ @Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String str=tfTxt.getText().trim();
//taContent.setText(str);
tfTxt.setText("");

try {
 
     dos.writeUTF(str);
     dos.flush();
    // dos.close();
}catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}

}
    //接收消息,连接到服务器以后启动该线程
private class ReceThread implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
try{
while(bConnected){
String str=dis.readUTF();
//System.out.println(str);
taContent.setText(taContent.getText()+str+"\n");
}
}catch(SocketException e){
System.out.println("退出了,bye");
}catch(EOFException e){
System.out.println("退出了,bye-bye");
}

catch(IOException e){
e.printStackTrace();
}
}

}


}
服务器端代码:public class CharServer { /**
 * @param guoyang
 */
ServerSocket ss=null;
boolean started=false;
List<client>list=new ArrayList<client>();//存储client端

//Queue<Thread> queue=new LinkedList<Thread>();
int a=0;//统计在线人数
public static void main(String[] args) {
       new CharServer().charServer();

}
    public void charServer(){
    
    
    
     try{
     ss=new ServerSocket(8888);
     started=true;
     }catch(Exception e){
     System.out.println("服务器已经启动");
     System.exit(0);
     }
        try {
            while(started){
     Socket s=ss.accept();
     a++;
     System.out.println("当前在线人数"+a);
     client c=new client(s);
    
         new Thread(c).start();
     list.add(c);            }
     } catch (IOException e) {
     e.printStackTrace();
     }finally{
     try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
     }
        
   
    }
    private class client implements Runnable{
     private Socket s=null;
     private DataInputStream br=null;
     private DataOutputStream bos=null;
     boolean start=false;
     public client(Socket s){
     this.s=s;
     try {
br=new DataInputStream(s.getInputStream());
bos=new DataOutputStream(s.getOutputStream());
     start=true;
     } catch (IOException e) {
e.printStackTrace();
}
     }
public void send(String str){
try {
bos.writeUTF(str);
bos.flush();
} catch (IOException e) {
list.remove(this);


}
}
    
     @Override
public void run() {
        
try {
while(start){
String str1=br.readUTF();
    for(int i=0;i<list.size();i++){
     client c=list.get(i);
     c.send(str1);
    
    }
      }
}catch (IOException e) {
     System.out.println("client closed");
     --a;
System.out.println("当前在线数"+a);
}finally{
try {
     br.close();
s.close();
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
    
    }
}