【问题标题】:Unable to shutdown a thread pool无法关闭线程池
【发布时间】:2015-12-14 18:56:51
【问题描述】:

我想在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)

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

【问题讨论】:

  • 你在ServerSocketset a timeout了吗?
  • 它有助于终止线程池,但我想避免使用 timeOut()(当然如果可行的话)。
  • 我只能想到两个选项第一个是超时,如前所述,本质上是每隔一段时间强制您的代码通过循环。第二种是中断从某个主线程发出接受调用的线程。
  • 有你的想法。但是,我想知道,为什么使用 countdownLatch 的解决方案不起作用?
  • 您不能通过Thread#interrupt() 中断accept() 调用(因为它不会抛出InterruptedException,因此无处处理中断),尽管您可以close() @987654330 @ 来自另一个线程,这应该会中断接受

标签: java multithreading sockets


【解决方案1】:

你有两个选择:

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

    while(countDownLatch.getCount() != 0) {
        try {
            socket = serverSocket.accept();
        } catch(SocketTimeoutException e) {
          //timeout occurred!
        }
    }
    
  2. 通过来自不同线程的ServerSocket#close() 关闭ServerSocket。这将引发SocketAcception,终止accept() 调用。可以在循环外部捕获此异常以轻松退出“侦听连接”行为。

【讨论】:

    猜你喜欢
    • 2011-04-28
    • 1970-01-01
    • 2014-03-10
    • 2023-02-02
    • 1970-01-01
    • 2010-11-29
    • 2011-12-05
    • 2014-07-08
    • 2013-06-15
    相关资源
    最近更新 更多