【问题标题】:unable to create new native thread?无法创建新的本机线程?
【发布时间】:2013-11-28 16:09:42
【问题描述】:

我正在实现一个UDP程序,客户端向另一个客户端发送消息,消息将存储在服务器中,当另一个客户端在线时,服务器将传递消息。

我为许多客户端使用了多线程

当我编译服务器类时,我得到了这个错误

Server started at port# 9776
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:693)
at SMSServer.main(SMSServer.java:49)

我的服务器类

import java.io.*; 
import java.net.*; 
import java.util.*;

public class SMSServer { 

   public static String message;
   public static InetAddress SenAdd;
   public InetAddress RectAdd;
   public static int portnum;
   private static int clientId = 0;


    public SMSServer(){
    message=" ";
    SenAdd=null;
    RectAdd=null;
    portnum=0;
    }

    public SMSServer(String msg, InetAddress SenAdd, int port, InetAddress RectAdd){
    this.message=msg;
    this.SenAdd=SenAdd;
    this.RectAdd=RectAdd;
    this.portnum=port;
    }

    public String getMsg(){return this.message;}
    public InetAddress getsAdd(){return this.SenAdd;}
    public InetAddress getrAdd(){return this.RectAdd;}
    public int getPort(){return this.portnum;}


public static void main(String args[]) throws IOException 
{ 


    DatagramSocket serverSocket = null;
    boolean listening = true;
int port = 9776;

// Setup socket 
try {
        serverSocket = new DatagramSocket(port); //it has to be from type serverSocket
        System.out.println("Server started at port# "+port);

            while (listening) {
    MultiServerThread1 server = new MultiServerThread1(serverSocket, clientId++); // thread for each client
            new Thread(server).start();
        } //end while    
    //serverSocket.close();
    } 

    catch (Exception e) {
        System.err.println("Could not start on port: "+port);
        System.exit(1);
    }



 /*  finally {
    if(serverSocket != null) 
        serverSocket.close();
    }*/

 //serverSocket.close(); 
}// end main 








} //end SMSserver

class MultiServerThread1  implements Runnable {

    private DatagramSocket serverSocket=null;
    private int clientId=0;
    public List<SMSServer> myList = new ArrayList<SMSServer>(); //list of object server 


public MultiServerThread1(DatagramSocket serverSocket,int clientId) {
    this.serverSocket = serverSocket;
            this.clientId = clientId;
}


    @Override
public void run() {


    try {

        byte[] receiveData = new byte[1024]; 
        byte[] sendData  = new byte[1024]; 



  while(true) 
    { 

      DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); 
      serverSocket.receive(receivePacket); // receive msgs from clients

      String segment = new String(receivePacket.getData());  // UDP packet
      InetAddress SendIP = receivePacket.getAddress(); // IP of client sender
      int port = receivePacket.getPort(); //port of client sender

      String[] parts = segment.split("-"); //to split the packet into
      String msg = parts[0]; //the message
      String msg2 = parts[1]; //the ip of the receiver of the msg

      InetAddress RecIP=InetAddress.getByName(msg2); //convert string to ip

      SMSServer client = new SMSServer(msg, SendIP, port, RecIP); // creat object server
      myList.add(client); // add the object to the list


    for(int i=0; i<myList.size(); i++){ // check all the objects


        if ( myList.get(i).getrAdd().equals(RecIP)) // check if object receiver IP is the same as the current client IP?
    {
       sendData=myList.get(i).getMsg().getBytes(); //get the msg from the object
       DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, RecIP, port);//send the msg to this dedicated client
       serverSocket.send(sendPacket); 
    }

    }//end for

    } //end while   

    } 
            catch (Exception e) {
    System.out.println("Error: " + e.getMessage());

            }

     System.out.println(Thread.currentThread().getName() + " just run.");       

} // run


} // MultiServerThread

所以我不知道这里有什么问题?!

【问题讨论】:

  • 那是运行时错误,不是编译错误,可能显示代码?
  • 错误消息 在运行时 我猜是很有描述性的。
  • 你能发布你的 SMSServer.java (尤其是 main 方法)吗?
  • @ElliottFrisch 我更新了问题
  • 在我看来,“while (listening)”循环只是不断创建新线程,直到系统崩溃。

标签: java sockets udp client-server


【解决方案1】:

该消息意味着您不能以任何理由创建更多线程。 根据我的经验,我有两个原因:

  1. 操作系统进程限制,每个用户或全局。
    • 您可以提高限制。
  2. 没有足够的内存来分配新的线程堆栈。
    • 您可以尝试分配更多内存。该内存不是堆的一部分,但我认为它与它的大小成正比。
    • 您可以降低新线程的堆栈大小。

【讨论】:

    【解决方案2】:

    你的问题在 SMSServer.java 附近 -

    MultiServerThread1 server = new MultiServerThread1(
            serverSocket, clientId++); // thread for each
                                       // client
    new Thread(server).start();
    

    请注意,您无需等待客户端即可开始新线程。因此,您最终会耗尽内存来启动进程。你真的需要等到来自客户端的receive 才能启动一个新线程。请参阅Multithreaded Servers in Java 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2015-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      相关资源
      最近更新 更多