在 JAVA 中将单线程服务器转换为多线程
Convert Single Threaded Server into Multi Threaded in JAVA
我需要将此单线程服务器转换为多线程服务器,以便我能够处理来自服务器的多个请求:
public class YASGP {
public static void main(String args[]) throws IOException {
ServerSocket server;
try{
server = new ServerSocket(5559);
System.out.println("Listening for connection on port 5559 ....");
while (true) {
Socket clientSocket = server.accept();
InputStreamReader isr = new InputStreamReader(clientSocket.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
new Thread(new WorkerRunnable(clientSocket)).start();
while (!line.isEmpty()) {
System.out.println("----- " + line);
if (!line.contains("OPTIONS")) {
// System.out.println("Non c'è nulla!!!");
} else {
timeS = line.substring(line.indexOf("timeS=") + 6, line.indexOf("&url"));
url = line.substring(line.indexOf("url=") + 4, line.lastIndexOf("¶m"));
param = line.substring(line.indexOf("¶m=") + 7, line.indexOf("HTTP"));
}
line = reader.readLine();
}
}
}
}catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
}
private static class RequestHandlingClass {
public RequestHandlingClass(Socket clientSocket) {
}
}
}
如何转换?感谢大家
从服务器中删除 'client processing' 代码如下
public static void main(String args[]) throws IOException {
ServerSocket server;
try{
server = new ServerSocket(5559);
System.out.println("Listening for connection on port 5559 ....");
while (true) {
Socket clientSocket = server.accept();
new Thread(new WorkerRunnable(clientSocket)).start();
}
}catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
}
然后您提取的代码进入 workerRunnable class 的 运行 方法。
我建议您使用 excutorService api.Since 它会在后台为您管理所有线程问题
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
//Your code here
});
我需要将此单线程服务器转换为多线程服务器,以便我能够处理来自服务器的多个请求:
public class YASGP {
public static void main(String args[]) throws IOException {
ServerSocket server;
try{
server = new ServerSocket(5559);
System.out.println("Listening for connection on port 5559 ....");
while (true) {
Socket clientSocket = server.accept();
InputStreamReader isr = new InputStreamReader(clientSocket.getInputStream());
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
new Thread(new WorkerRunnable(clientSocket)).start();
while (!line.isEmpty()) {
System.out.println("----- " + line);
if (!line.contains("OPTIONS")) {
// System.out.println("Non c'è nulla!!!");
} else {
timeS = line.substring(line.indexOf("timeS=") + 6, line.indexOf("&url"));
url = line.substring(line.indexOf("url=") + 4, line.lastIndexOf("¶m"));
param = line.substring(line.indexOf("¶m=") + 7, line.indexOf("HTTP"));
}
line = reader.readLine();
}
}
}
}catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
}
private static class RequestHandlingClass {
public RequestHandlingClass(Socket clientSocket) {
}
}
}
如何转换?感谢大家
从服务器中删除 'client processing' 代码如下
public static void main(String args[]) throws IOException {
ServerSocket server;
try{
server = new ServerSocket(5559);
System.out.println("Listening for connection on port 5559 ....");
while (true) {
Socket clientSocket = server.accept();
new Thread(new WorkerRunnable(clientSocket)).start();
}
}catch (IOException e) {
System.out.println("Could not listen on port: 4001");
}
}
然后您提取的代码进入 workerRunnable class 的 运行 方法。
我建议您使用 excutorService api.Since 它会在后台为您管理所有线程问题
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
//Your code here
});