AsynchronousServerSocketChannel最后块永远不会执行

时间:2014-04-25 11:12:47

标签: java asynchronous serversocket socketchannel

我创建了AsynchronousServerSocketChannel,一切都很好,直到我想要关闭服务器套接字套接字。 我在客户端上创建了AsynchronousSocketChannel,它带有特殊的消息,调用函数来关闭服务器和所有套接字通道,服务器是关闭的,但是finnally block永远不会执行为什么?

检查代码:

 public void openServerSocket(int sport) {


    port = sport;
    policyFile = createPolicy(new String[]{"*:"+port});
    try {

        //group = AsynchronousChannelGroup.withThreadPool(Executors.newSingleThreadExecutor());
        //group.awaitTermination(1, TimeUnit.SECONDS);
        server = AsynchronousServerSocketChannel.open();
        server.bind(new InetSocketAddress(sport));


        server.accept(null, this);

       while(isOpen) {

            //System.out.println("running server socket");

        }
    }catch(Exception ex) {
        this.message = ex.getMessage();
        System.out.println("Server: " + ex.getMessage());
    }finally {

        System.out.println("Finall serversocket closing!");
        for(SocketHandler sh : sockets) {
            sh.close();
        }

        if(server != null) {
            try {
                server.close();
                server = null;
            }catch(Exception ex2) {
                this.message = ex2.getMessage();
            }
        }


        sockets.clear();
        sockets = null;
        //this.message = "server socket closed";

        System.out.println("server socket down");
    }

}

@Override
public void completed(AsynchronousSocketChannel socket, Void attachment) {

    System.out.println("new connection");
    SocketHandler sh = new SocketHandler(socket, this);

    server.accept(null, this);
}

@Override
public void failed(Throwable exc, Void attachment) {

    System.out.println("Server Failed accept:" + exc.getMessage());
}


public void closeServerSocket() {

    System.out.println("Closing server socket! " + server);
    this.isOpen = false;

}

因此函数closeServerSocket被调用,我将false设置为变量isOpen,它保持循环,但似乎当我将is设置为isOpen var时,仍然是循环并且最终永远不会被执行?

1 个答案:

答案 0 :(得分:1)

这看起来像是几个线程之间的通信问题。从代码中我猜你的while(isOpen)循环运行在与你的类的closeServerSocket()方法不同的线程中。要验证这一点,请在打印报表中添加Thread.currentThread()

如果是这种情况,您可以通过将isOpen声明为volatile来解决问题。如果没有volatile,则无法保证,如果和任何其他线程将看到写入的值。使用volatile可以保证(我对happens-before的解释)线程访问变量以后(根据挂钟时间)将看到更改的值。

而不是volatile您可能更喜欢AtomicBoolean