其他分享
首页 > 其他分享> > 揭秘幸运飞艇怎么玩才稳赢456码稳赢公式揭秘给大家

揭秘幸运飞艇怎么玩才稳赢456码稳赢公式揭秘给大家

作者:互联网

  PooledByteBuf的初始化过程分为两个步骤:创建实例;初始化内存。这两个步骤的代码如下:

复制代码507383170
    protected PooledByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
        super(maxCapacity);
        this.recyclerHandle = recyclerHandle;
    }

    void init(PoolChunk<T> chunk, long handle, int offset, int length, int maxLength, PoolThreadCache cache) {
        init0(chunk, handle, offset, length, maxLength, cache);
    }

    private void init0(PoolChunk<T> chunk, long handle, int offset, int length, int maxLength, PoolThreadCache cache) {
        assert handle >= 0;
        assert chunk != null;

        this.chunk = chunk;
        memory = chunk.memory;
        allocator = chunk.arena.parent;
        this.cache = cache;
        this.handle = handle;
        this.offset = offset;
        this.length = length;
        this.maxLength = maxLength;
        tmpNioBuf = null;
    }
复制代码

  创建实例时调用的构造方法只是为maxCapacity和recyclerHandler属性赋值,构造方法是protected,不打算暴露到外面。派生类都提供了newInstance方法创建实例,以PooledHeapByteBuf为例,它的newInstance方法实现如下:

复制代码
 1     private static final Recycler<PooledHeapByteBuf> RECYCLER = new Recycler<PooledHeapByteBuf>() {
 2         @Override
 3         protected PooledHeapByteBuf newObject(Handle handle) {
 4             return new PooledHeapByteBuf(handle, 0);
 5         }
 6     };
 7 
 8     static PooledHeapByteBuf newInstance(int maxCapacity) {
 9         PooledHeapByteBuf buf = RECYCLER.get();
10         buf.reuse(maxCapacity);
11         return buf;
12     }
复制代码

  这里的newInstance使用RECYCLER创建实例对象。Recycler<T>是一个轻量级的,支持循环使用的对象池。当对象池中没有可用对象时,会在第4行使用构造方法创建一个新的对象。

 

  init调用init0初始化数据内存,init0方法为几个内存相关的关键属性赋值:

  内存初始化完成之后,这个PooledByteBuf可使用的内存范围是memory内存中[offset, offset+length)。idx方法可以把ByteBuf的索引转换成memory的索引:

1     protected final int idx(int index) {
2         return offset + index;
3     }

 

重新分配内存

  和前面讲过的ByteBuf实现一样,PooledByteBuf也需要使用capacity(int newCapacity)改变内存大小,也会涉及到把数据从旧内存中复制到新内存的问题。也就是说,要解决的问题是一样的,只是具体实现的差异。

复制代码
 1     @Override
 2     public final ByteBuf capacity(int newCapacity) {
 3         checkNewCapacity(newCapacity);
 4 
 5         // If the request capacity does not require reallocation, just update the length of the memory.
 6         if (chunk.unpooled) {
 7             if (newCapacity == length) {
 8                 return this;
 9             }
10         } else {
11             if (newCapacity > length) {
12                 if (newCapacity <= maxLength) {
13                     length = newCapacity;
14                     return this;
15                 }
16             } else if (newCapacity < length) {
17                 if (newCapacity > maxLength >>> 1) {
18                     if (maxLength <= 512) {
19                         if (newCapacity > maxLength - 16) {
20                             length = newCapacity;
21                             setIndex(Math.min(readerIndex(), newCapacity), Math.min(writerIndex(), newCapacity));
22                             return this;
23                         }
24                     } else { // > 512 (i.e. >= 1024)
25                         length = newCapacity;
26                         setIndex(Math.min(readerIndex(), newCapacity), Math.min(writerIndex(), newCapacity));
27                         return this;
28                     }
29                 }
30             } else {
31                 return this;
32             }
33         }
34 
35         // Reallocation required.
36         chunk.arena.reallocate(this, newCapacity, true);
37         return this;
38     }
复制代码

标签:int,chunk,newCapacity,456,length,内存,offset,稳赢,揭秘
来源: https://www.cnblogs.com/aa1212/p/11706352.html