加入支持多线程(线程池)
package com.zibra.server.thread;
import java.net.Socket;
public class SocketThread implements Runnable {
// 线程参数
private Socket socket;
public SocketThread(Socket socket) {
this.socket = socket;
}
public void run() {
try {
System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
// 执行业务代码
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 断开连接
if (socket != null) {
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
服务器的主线程负责接收客户的连接, 每次接收到一个客户连接, 就会创建一个工作线程, 由它负责与客户的通信.
package com.zibra.server;
impo