如何关闭多线程服务器?

huangapple go评论70阅读模式
英文:

How do I close a multithreaded server?

问题

以下是您提供的代码的中文翻译部分:

public class TaskListServer {
    public static List<String> taskList = new ArrayList<>();

    public static String uniqueString(){
        return String.join(", ", taskList);
    }

    public static void addTasks(String task) {
        taskList.add(task);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        try {

            ServerSocket serverSocket = new ServerSocket(4242);
            while (true) {
                Socket clientSocket = serverSocket.accept();
                ThreadedServer clientThread = new ThreadedServer(clientSocket);
                new Thread(clientThread).start();
            }

        }catch (IOException e) {
            e.printStackTrace();
        }

        sc.close();
    }
}
英文:

So I have a program that clients can enter any type of tasks and other clients can check it as well but I don't know how to close the loop that tries to accept new connetions to the server.
What I want is when I close the last client running using a command I wrote I want the server to close as well.

Can anyone help?

public class TaskListServer {
    public static List&lt;String&gt; taskList = new ArrayList&lt;&gt;();

    public static String uniqueString(){
        return String.join(&quot;, &quot;, taskList);
    }

    public static void addTasks(String task) {
        taskList.add(task);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        try {

            ServerSocket serverSocket = new ServerSocket(4242);
            while (true) {
                Socket clientSocket = serverSocket.accept();
                ThreadedServer clientThread = new ThreadedServer(clientSocket);
                new Thread(clientThread).start();
            }

        }catch (IOException e) {
            e.printStackTrace();
        }

        sc.close();
    }

答案1

得分: 0

不要使用无限循环(while(true))。

private static boolean acceptNewClients = true;

public static void flipAcceptNewClients() {
    acceptNewClients = !acceptNewClients;
}

public static void main(String[] args) {
    /* 你的代码在这里 */
    while (acceptNewClients) {
        Socket clientSocket = serverSocket.accept();
        ThreadedServer clientThread = new ThreadedServer(clientSocket);
        new Thread(clientThread).start();
    }
    /* 你的代码在这里 */
}
英文:

Don't use an infinity loop (while(true)).

private static boolean acceptNewClients = true;
	
	public static void flipAcceptNewClients() {
		acceptNewClients = !acceptNewClients;
	}
	
	public static void main(String[] args) {
        /* your code here */
		while (acceptNewClients) {
			Socket clientSocket = serverSocket.accept();
			ThreadedServer clientThread = new ThreadedServer(clientSocket);
			new Thread(clientThread).start();
		}
        /* your code here */
	}

huangapple
  • 本文由 发表于 2020年10月24日 06:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64508061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定