多线程的聊天服务器,每个用户一个线程,同时开一个Socket连接,然后把这个线程实例的句柄保存在一个数组中,每当有新的用户到来时,循环得到数组中的线程实例,调用每一个线程的send函数通知其他人,现在的问题是,如果用户多了,有时候会挂起,不知道什么原因,请帮助分析一下,另外,这样在一个线程中调用另一个线程实例类的一个函数,这个函数会立即执行吗?还是要等另一个线程被激活后才执行?

解决方案 »

  1.   

    1),用户多了会挂起,可能是因为CPU时间不够用,自动等待.
    2),不会立即执行,所有的线程只是会进入ready状态,只有系统决定何时start.
      

  2.   

    用不用每个用户一个SOCKET呢?如果服务端只用一个SOCKET,每个客户端的SOCKET都向这一个SOCKET通信,利用信息头来区分用户也可以。因为聊天这种应用,客户端的通信不可能太密集,所以应该够用吧。
      

  3.   

    给你看看我写的聊天程序,也许对你有帮助:服务器端:import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.event.*;class ServerTest extends JFrame
    {
        JTextArea ta=new JTextArea();
        JScrollPane sp=new JScrollPane(ta);
        OutputStream os;
        PrintWriter pw;
        Vector v=new Vector();
        public ServerTest(String title)
        {
            super(title);
            setSize(400, 300);
            setLocation(380, 260);
            ta.setEnabled(false);
            addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    //pw.println("我要走了!");
                    //pw.flush();
                    System.exit(0);
                }
            });
            add(sp);
            setVisible(true);
            init();
        }
        public void init()
        {
            try
            {
                ServerSocket ss=new ServerSocket(6000);
                while(true)
                {
                    Socket s = ss.accept();
                    InputStream is = s.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    os = s.getOutputStream();
                    pw = new PrintWriter(os);
                    v.add(pw);
                    Chat c=new Chat(br);
                    c.start();
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }    class Chat extends Thread
        {
            BufferedReader br;
            public Chat(BufferedReader br)
            {
                this.br=br;
            }
            public void run()
            {
                while (true)
                {
                    try
                    {
                        String str = br.readLine();
                        if(str.equals("我要走了!"))
                        {
                            break;
                        }
                        else
                        {
                         Iterator it = v.iterator();
                         while (it.hasNext())
                         {
                             PrintWriter pw = (PrintWriter) it.next();
                             pw.println(str);
                             pw.flush();
                             //System.out.println(it.next());
                         }
                         ta.append(str);
                         ta.append("\n");
                        }
                    }catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }    public static void main(String[] args) throws Exception
        {
            ServerTest st = new ServerTest("服务器端");
            //st.ta.setEnabled(false);
        }}
    客户端:import java.io.*;
    import java.net.*;
    import javax.swing.*;
    import java.awt.event.*;class ClientTest extends JFrame implements ActionListener
    {
        Socket s;
        JTextField tf=new JTextField();
        JTextArea ta=new JTextArea();
        JScrollPane sp=new JScrollPane(ta);
        JButton btn=new JButton("发送");
        OutputStream os;
        PrintWriter pw;
        BufferedReader br;
        static String name="";
        public ClientTest(String title)
        {
            super(title);
            setSize(400,300);
            setLocation(380,260);
            setLayout(null);
            addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    pw.println("我要走了!");
                    pw.flush();
                    System.exit(0);
                }
            });
            sp.setBounds(0,0,390,230);
            add(sp);
            tf.setBounds(10,240,250,20);
            add(tf);
            btn.setBounds(300,240,70,20);
            add(btn);
            btn.addActionListener(this);
            setVisible(true);
            init();
        }
        public void init()
        {
            try
            {
                s=new Socket("127.0.0.1",6000);
                InputStream is = s.getInputStream();
                br = new BufferedReader(new InputStreamReader(is));
                os = s.getOutputStream();
                pw = new PrintWriter(os);
                pw.println(name+"上线了");
                pw.flush();
                Chat c=new Chat();
                c.start();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getActionCommand().equals("发送"))
            {
                try
                {
                    String str=tf.getText();
                    pw.println(name+" 说: "+str);
                    tf.setText("");
                    pw.flush();
                    tf.requestFocus(true);
                }catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        class Chat extends Thread
        {
            public void run()
            {
                while(true)
                {
                    try
                    {
                     //if(br.readLine().equals("我要走了!"))
                        //{
                            //break;
                        //}
                        //else
                        //{
                         ta.append(br.readLine());
                         ta.append("\n");
                        //}
                    } catch (Exception ex) {                }
                }
            }
        }
        public static void main(String[] args)
        {
            if(args.length!=1)
            {
                JOptionPane.showMessageDialog(null, "请输入一个名称!", "提示", 2);
                System.exit(0);
            }
            name=args[0];
            ClientTest ct=new ClientTest(name+"的聊天室");
            ct.ta.setEnabled(false);
            ct.tf.requestFocus(true);
        }
    }