当我启动服务器时,出现错误,但服务器仍然工作正常!
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:658) at Gateway.main(Server.java:21)
public static void main(String args[]) {
try {
DatagramSocket serverSocket = new DatagramSocket(Integer.parseInt(args[0]));;
System.out.println("Waiting for clients");
while (true) {
Child cServer = new Child(serverSocket);
cServer.start();
}
} catch (IOException ex) {
System.out.println(ex);
}
}
}
子类
public Child(DatagramSocket ssocket) {
this.socket = ssocket;
}
public void run() {
while(true)
{
byte[] receiveData = new byte[1024];
编辑: 我尝试像这个例子一样,它在这里工作正常
public static void main(String[] args) throws IOException {
server = new MyServer();
server.runServer();
}
private void runServer() {
int serverPort = 8071;
try {
System.out.println("Starting Server");
serverSocket = new ServerSocket(serverPort);
while(true) {
System.out.println("Waiting for request");
try {
Socket s = serverSocket.accept();
System.out.println("Processing request");
executorService.submit(new ServiceRequest(s));
} catch(IOException ioe) {
System.out.println("Error accepting connection");
ioe.printStackTrace();
}
}
}catch(IOException e)
最佳答案
您正在无限 while 循环中创建新的 Child 对象,直到耗尽所有可用内存。
关于java - 无法创建新的 native 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21396261/