gpt4 book ai didi

Java 多线程服务器未按预期工作

转载 作者:行者123 更新时间:2023-12-01 20:56:38 25 4
gpt4 key购买 nike

我正在尝试编写一个多线程服务器,它应该能够一次接受多个 HTTP 请求。

服务器代码:

package test.thread.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MyServer implements Runnable {
private int serverPort = 8080;
private ServerSocket serverSocket;
private Thread runningThread;
private boolean isStopped;

public MyServer(int port){
this.serverPort = port;
}


@Override
public void run() {
synchronized (this) {
this.runningThread = Thread.currentThread();
}

openServerSocket();


while(!isStopped){

Socket clientSocket = null;

try {
clientSocket = this.serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}


//start a new thread for processing each request
new Thread(new RequestHandler(clientSocket)).start();
}

}

public synchronized void stop(){
this.isStopped = true;
try {
this.serverSocket.close();
} catch (IOException e) {
throw new RuntimeException("Error closing server", e);
}
}


private void openServerSocket(){
try {
this.serverSocket = new ServerSocket(this.serverPort);
} catch (IOException e) {
e.printStackTrace();
}
}

}

处理请求的工作人员:

它从输入流读取数据并打印它。之后它应该进入 30 秒 sleep [代表一些不需要 CPU 的工作]。 hibernate 时间过后,服务器将响应客户端。

package test.thread.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Date;

public class RequestHandler implements Runnable {
Socket clientSocket;

static int counter = 0;

public RequestHandler(Socket clientSocket){
this.clientSocket = clientSocket;
}

@Override
public void run() {

try {
InputStream input = this.clientSocket.getInputStream();
OutputStream output = this.clientSocket.getOutputStream();


DataInputStream inFromClient = new DataInputStream(input);
System.out.println(new Date()+": " +Thread.currentThread().getName() + " - Started : "+inFromClient.readUTF());

Thread.sleep(30000);

/*output.write(("HTTP/1.1 200 OK\n\n<html><body>" +
"Multi-threaded Server " +
"</body></html>").getBytes());*/

DataOutputStream outFromServer = new DataOutputStream(output);
outFromServer.writeUTF("Output");
outFromServer.flush();

output.close();
input.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

HTTP 客户端:

客户端不关心服务器 hibernate 时间,会连续发送 5 个请求。

package test.thread.server;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

public class MyClient {

public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
for(int i=0; i<5; i++){
Socket clientSocket = new Socket("localhost", 8080);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());


outToServer.writeUTF("Input");
outToServer.flush();

String output = inFromServer.readUTF();
System.out.println(new Date()+": "+output);
clientSocket.close();
}
}

}

现在我期望服务器在一个线程进入休眠状态时处理下一个客户端请求。但不幸的是,服务器只有在处理完前一个请求后才接受下一个请求。

预期o/p:

在为下一个请求打印“Thread-X - Started:Input”之前,服务器不应等待当前请求进程完成。

当前营业额:

Server: Wed Feb 15 18:17:06 IST 2017: Thread-1 - Started : Input
Client: Wed Feb 15 18:17:36 IST 2017: Output
Server:Wed Feb 15 18:17:36 IST 2017: Thread-2 - Started : Input
Client:Wed Feb 15 18:18:06 IST 2017: Output
Server:Wed Feb 15 18:18:06 IST 2017: Thread-3 - Started : Input
Client:Wed Feb 15 18:18:36 IST 2017: Output
Server:Wed Feb 15 18:18:43 IST 2017: Thread-4 - Started : Input
Client:Wed Feb 15 18:19:13 IST 2017: Output
Server:Wed Feb 15 18:19:13 IST 2017: Thread-5 - Started : Input
Client:Wed Feb 15 18:19:43 IST 2017: Output

可能是什么问题?

最佳答案

我相信问题出在你的客户端上。客户端仅在前一个请求得到答复后才发送下一个请求。 inFromServer.readUTF() 会阻塞,直到数据可用为止,只有当您的服务器发送实际答案时才会出现这种情况。

我建议你调试你的程序。

关于Java 多线程服务器未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42250548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com