如何关闭多线程服务器?

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

How do I close a multithreaded server?

问题

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

  1. public class TaskListServer {
  2. public static List<String> taskList = new ArrayList<>();
  3. public static String uniqueString(){
  4. return String.join(", ", taskList);
  5. }
  6. public static void addTasks(String task) {
  7. taskList.add(task);
  8. }
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. try {
  12. ServerSocket serverSocket = new ServerSocket(4242);
  13. while (true) {
  14. Socket clientSocket = serverSocket.accept();
  15. ThreadedServer clientThread = new ThreadedServer(clientSocket);
  16. new Thread(clientThread).start();
  17. }
  18. }catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. sc.close();
  22. }
  23. }
英文:

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?

  1. public class TaskListServer {
  2. public static List&lt;String&gt; taskList = new ArrayList&lt;&gt;();
  3. public static String uniqueString(){
  4. return String.join(&quot;, &quot;, taskList);
  5. }
  6. public static void addTasks(String task) {
  7. taskList.add(task);
  8. }
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. try {
  12. ServerSocket serverSocket = new ServerSocket(4242);
  13. while (true) {
  14. Socket clientSocket = serverSocket.accept();
  15. ThreadedServer clientThread = new ThreadedServer(clientSocket);
  16. new Thread(clientThread).start();
  17. }
  18. }catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. sc.close();
  22. }

答案1

得分: 0

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

  1. private static boolean acceptNewClients = true;
  2. public static void flipAcceptNewClients() {
  3. acceptNewClients = !acceptNewClients;
  4. }
  5. public static void main(String[] args) {
  6. /* 你的代码在这里 */
  7. while (acceptNewClients) {
  8. Socket clientSocket = serverSocket.accept();
  9. ThreadedServer clientThread = new ThreadedServer(clientSocket);
  10. new Thread(clientThread).start();
  11. }
  12. /* 你的代码在这里 */
  13. }
英文:

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

  1. private static boolean acceptNewClients = true;
  2. public static void flipAcceptNewClients() {
  3. acceptNewClients = !acceptNewClients;
  4. }
  5. public static void main(String[] args) {
  6. /* your code here */
  7. while (acceptNewClients) {
  8. Socket clientSocket = serverSocket.accept();
  9. ThreadedServer clientThread = new ThreadedServer(clientSocket);
  10. new Thread(clientThread).start();
  11. }
  12. /* your code here */
  13. }

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:

确定