BootstrapTest.java 文件源码

java
阅读 31 收藏 0 点赞 0 评论 0

项目:netty4study 作者:
@Test(timeout = 10000)
public void testConnectDeadLock() throws Exception {
    EventLoopGroup groupA = new LocalEventLoopGroup(1);
    EventLoopGroup groupB = new LocalEventLoopGroup(1);

    try {
        ChannelInboundHandler dummyHandler = new DummyHandler();

        final Bootstrap bootstrapA = new Bootstrap();
        bootstrapA.group(groupA);
        bootstrapA.channel(LocalChannel.class);
        bootstrapA.handler(dummyHandler);

        final Bootstrap bootstrapB = new Bootstrap();
        bootstrapB.group(groupB);
        bootstrapB.channel(LocalChannel.class);
        bootstrapB.handler(dummyHandler);

        List<Future<?>> bindFutures = new ArrayList<Future<?>>();

        // Try to connect from each other.
        for (int i = 0; i < 1024; i ++) {
            bindFutures.add(groupA.next().submit(new Runnable() {
                @Override
                public void run() {
                    bootstrapB.connect(LocalAddress.ANY);
                }
            }));

            bindFutures.add(groupB.next().submit(new Runnable() {
                @Override
                public void run() {
                    bootstrapA.connect(LocalAddress.ANY);
                }
            }));
        }

        for (Future<?> f: bindFutures) {
            f.sync();
        }
    } finally {
        groupA.shutdownGracefully();
        groupB.shutdownGracefully();
        groupA.terminationFuture().sync();
        groupB.terminationFuture().sync();
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号