创建主线程
Thread desktopServerThread = new Thread(new Server());
desktopServerThread.start();
public class Server implements Runnable {
public void run() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4321);
while (true) {
Socket socket = serverSocket.accept();
/* 监听到客户端访问,则创建线程提供服务 */
ServerWorker worker = new ServerWorker(socket);
new Thread(worker).start();
}
}
class ServerWorker implements Runnable {
private String result = "";
private Socket socket = null;
ServerWorker(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
执行操作。。。。。。。。
}
}
问题:
1:我的主线程接到访问后,会创建多个线程来提供服务吗?
2:创建的多个线程后,系统会自动回收吗? 如果访问过多的话,,怎么处理???
小唯快跑啊
相关分类