我创建了AsynchronousServerSocketChannel,一切都很好,直到我想关闭服务器套接字套接字为止。
我在客户机上创建带有特殊消息的AsynchronousSocketChannel,该消息调用函数来关闭服务器和所有套接字通道,服务器已关闭,但最终块从未执行过,为什么?

验证码:

 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变量,但是当我将false设置为isOpen var时,似乎仍在循环并且最终从未执行?

最佳答案

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

在这种情况下,您可以通过声明isOpenvolatile来纠正这种情况。如果没有volatile,则不能保证是否以及何时有其他线程看到写入的值。使用volatile可以确保(我对happens-before的解释)稍后访问该变量的线程(根据挂钟时间)将看到更改后的值。

代替volatile,您可能更喜欢AtomicBoolean

关于java - AsynchronousServerSocketChannel最终块永远不会执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23291426/

10-09 17:30