我试图创建多线程服务器,它将接收来自不同客户端的消息,然后发回消息。我正在使用执行服务器来管理线程的创建。但我不确定我是否做得对,我以前没有使用过执行人服务吗?我对这条线
executor.execute(new Handler(client));
处理程序有问题是抽象的,无法初始化?如何解决?执行服务会解决绑定端口的问题吗?它会像客户的请求队列一样吗?提前谢谢
package serverx;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Handler;
public class ServerX {
public static void main(String [] args){
ExecutorService executor = Executors.newFixedThreadPool(30);
ServerSocket server;
try{
server= new ServerSocket(5555);
System.out.println ("Server started!");
while(true) {
try{
Socket client = server.accept();
//Thread t= new Thread (new Handler(client));
//t.start();
executor.execute(new Handler(client));
}
catch (IOException e) {
e.printStackTrace();
}
}
}catch (IOException el){
el.printStackTrace();
}
}
}
处理程序:
public class Handler implements Runnable{
private Socket client;
public Handler(Socket client){
this.client=client;}
public void run(){
//............
}
}