Java AIO并发阻止

发布于 2025-01-22 00:03:05 字数 489 浏览 1 评论 0原文

如果频道未关闭,将会有一个阻塞问题,但是无法通过闭合

测试重复使用该链接

C:\Users\hxm>ab -k -c100 -n1000 http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
apr_pollset_poll: The timeout specified has expired (70007)
Total of 100 requests completed

If the channel is not closed, there will be a blocking problem, but the link cannot be reused by closing

test

C:\Users\hxm>ab -k -c100 -n1000 http://127.0.0.1:8080/
This is ApacheBench, Version 2.3 <$Revision: 1843412 
gt;
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 100 requests
apr_pollset_poll: The timeout specified has expired (70007)
Total of 100 requests completed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温馨耳语 2025-01-29 00:03:05

来源

public class HSocket implements Runnable {

    private Integer port = 8080;

    private static final Logger LOGGER = Logger.getLogger(HSocket.class.getName());

    private AsynchronousServerSocketChannel ssc;

    private CountDownLatch countDownLatch = new CountDownLatch(1);

    public void run() {
        try {
            AsynchronousChannelGroup cg = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(8));
            ssc = AsynchronousServerSocketChannel.open(cg);
            ssc.bind(new InetSocketAddress(port), 100);
            ssc.accept(this, new CompletionHandler<AsynchronousSocketChannel, HSocket>() {
                @Override
                public void completed(AsynchronousSocketChannel channel, HSocket attachment) {
                    try {
                        ssc.accept(attachment, this);
                    } finally {
                        String body = generateErrorResponse("<h1>aaa</h1>");
                        channel.write(ByteBuffer.wrap(body.getBytes()));
                    }
                }

                @Override
                public void failed(Throwable exc, HSocket attachment) {
                    ssc.accept(attachment, this);
                }
            });
            LOGGER.log(Level.INFO, "server port " + port);
            countDownLatch.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        new Thread(new HSocket()).start();
    }

    private String generateErrorResponse(String message) {
        return "HTTP/1.1 200 OK\r\n" +
                "Content-type:text/html\r\n" +
                "Connection:keep-alive\r\n" +
                "Content-Length:" + message.length() + "\r\n" +
                "\r\n" +
                message;
    }

}

source

public class HSocket implements Runnable {

    private Integer port = 8080;

    private static final Logger LOGGER = Logger.getLogger(HSocket.class.getName());

    private AsynchronousServerSocketChannel ssc;

    private CountDownLatch countDownLatch = new CountDownLatch(1);

    public void run() {
        try {
            AsynchronousChannelGroup cg = AsynchronousChannelGroup.withThreadPool(Executors.newFixedThreadPool(8));
            ssc = AsynchronousServerSocketChannel.open(cg);
            ssc.bind(new InetSocketAddress(port), 100);
            ssc.accept(this, new CompletionHandler<AsynchronousSocketChannel, HSocket>() {
                @Override
                public void completed(AsynchronousSocketChannel channel, HSocket attachment) {
                    try {
                        ssc.accept(attachment, this);
                    } finally {
                        String body = generateErrorResponse("<h1>aaa</h1>");
                        channel.write(ByteBuffer.wrap(body.getBytes()));
                    }
                }

                @Override
                public void failed(Throwable exc, HSocket attachment) {
                    ssc.accept(attachment, this);
                }
            });
            LOGGER.log(Level.INFO, "server port " + port);
            countDownLatch.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        new Thread(new HSocket()).start();
    }

    private String generateErrorResponse(String message) {
        return "HTTP/1.1 200 OK\r\n" +
                "Content-type:text/html\r\n" +
                "Connection:keep-alive\r\n" +
                "Content-Length:" + message.length() + "\r\n" +
                "\r\n" +
                message;
    }

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文