JavaInterview JavaInterview
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)

『Java面试+Java学习』
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)
  • 指南
  • 简历

  • Java

  • 面试

  • 算法

  • sourcecode
  • netty
JavaInterview.cn
2022-05-10
目录

Netty代码写法总结2Java

文章发布较早,内容可能过时,阅读注意甄别。

# 总结2

# 构造上下文

DNS解析,Map转Set

    private final Set<Future<DnsResponse>> queriesInProgress =
            Collections.newSetFromMap(new IdentityHashMap<Future<DnsResponse>, Boolean>());
    private List<InetAddress> resolvedAddresses;
    private StringBuilder trace;
    private int allowedQueries;
    private boolean triedCNAME;

    DnsNameResolverContext(DnsNameResolver parent, String hostname, int port, Promise<InetSocketAddress> promise) {
        this.parent = parent;
        this.promise = promise;
        this.hostname = hostname;
        this.port = port;

        maxAllowedQueries = parent.maxQueriesPerResolve();
        resolveAddressTypes = parent.resolveAddressTypesUnsafe();
        allowedQueries = maxAllowedQueries;
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 增加链路StringBuilder


    private void addTrace(InetSocketAddress nameServerAddr, String msg) {
        if (trace == null) {
            trace = new StringBuilder(128);
        }

        trace.append(StringUtil.NEWLINE);
        trace.append("\tfrom ");
        trace.append(nameServerAddr);
        trace.append(": ");
        trace.append(msg);
    }
1
2
3
4
5
6
7
8
9
10
11
12

# 定义组Group的概念

/**
 * A {@link NameResolverGroup} of {@link DnsNameResolver}s.
 */
public final class DnsNameResolverGroup extends NameResolverGroup<InetSocketAddress> {

    private final ChannelFactory<? extends DatagramChannel> channelFactory;
    private final InetSocketAddress localAddress;
    private final Iterable<InetSocketAddress> nameServerAddresses;

    public DnsNameResolverGroup(
            Class<? extends DatagramChannel> channelType,
            InetSocketAddress nameServerAddress) {
        this(channelType, ANY_LOCAL_ADDR, nameServerAddress);
    }

    
/**
 * Creates and manages {@link NameResolver}s so that each {@link EventExecutor} has its own resolver instance.
 */
public abstract class NameResolverGroup<T extends SocketAddress> implements Closeable {


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 移位操作及for(;;)死循环


    private int allocateId() {
        int id = ThreadLocalRandom.current().nextInt(parent.promises.length());
        final int maxTries = parent.promises.length() << 1;
        int tries = 0;
        for (;;) {
            if (parent.promises.compareAndSet(id, null, this)) {
                return id;
            }

            id = id + 1 & 0xFFFF;

            if (++ tries >= maxTries) {
                throw new IllegalStateException("query ID space exhausted: " + question);
            }
        }
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
微信 支付宝
#Java
最近更新
01
1633. 各赛事的用户注册率 Java
07-02
02
1636. 按照频率将数组升序排序 Java
07-02
03
1639. 通过给定词典构造目标字符串的方案数 Java
07-02
更多文章>
Theme by Vdoing | Copyright © 2019-2025 JavaInterview.cn
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式