无法关闭线程池

时间:2015-12-14 18:56:52

标签: java multithreading sockets

我想在countDownLatch.getCount()为零时关闭服务器,但不是关闭,而是等待接受套接字连接。这是处理传入TCP请求的服务器代码。

public static void start(File configFile, ReplyListener replyListener, CountDownLatch countDownLatch) {
            try {
                Server.init(configFile);
                while (countDownLatch.getCount() != 0) {
                    //try {
                        Server.socket = serverSocket.accept();
                        Message message = new Message();
                        message.setSocket(Server.socket);
                        Server.pool.submit(new ServerTaskCallable(message.receive()).setReplyListener(replyListener));
                    /*} catch (SocketTimeoutException e){
                        System.out.println("Finished testing");
                    }*/
                }
                Server.socket.close();
                serverSocket.close();
                Server.pool.shutdownNow();
            } catch (IOException e) {
                logger.error("Cannot start a server. The server thread will be terminated. Exception: ", e);
                System.exit(0);
            }
        }

这是线程dum:

"pool-1-thread-1@651" prio=5 tid=0xe nid=NA runnable
  java.lang.Thread.State: RUNNABLE
      at java.net.PlainSocketImpl.socketAccept(PlainSocketImpl.java:-1)
      at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:404)
      at java.net.ServerSocket.implAccept(ServerSocket.java:545)
      at java.net.ServerSocket.accept(ServerSocket.java:513)
      at model.server.Server.start(Server.java:42)
      at model.MainTest.lambda$main$3(MainTest.java:30)
      at model.MainTest$$Lambda$2.2012232625.run(Unknown Source:-1)
      at java.lang.Thread.run(Thread.java:745)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)

"pool-3-thread-1@1142" prio=5 tid=0x12 nid=NA waiting
  java.lang.Thread.State: WAITING
      at sun.misc.Unsafe.park(Unsafe.java:-1)
      at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
      at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
      at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
      at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)

如何正确终止服务器线程池?

1 个答案:

答案 0 :(得分:1)

您有两种选择:

  1. 通过ServerSocket#setSoTimeout(int)ServerSocket上设置超时。每次发生超时时,这将导致accept()抛出SocketTimeoutException。您需要捕获循环中的异常,以确保您的服务器一直在监听连接:

    while(countDownLatch.getCount() != 0) {
        try {
            socket = serverSocket.accept();
        } catch(SocketTimeoutException e) {
          //timeout occurred!
        }
    }
    
  2. 从另一个帖子中通过ServerSocket#close()关闭ServerSocket。这会抛出SocketAcception,终止accept()来电。可以在外部循环中捕获此异常,以便轻松退出"侦听连接"行为。