Home / Class/ AllocationThread Class — netty Architecture

AllocationThread Class — netty Architecture

Architecture documentation for the AllocationThread class in PooledByteBufAllocatorTest.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  6bb945aa_60fd_1a15_e603_8ad95417c6e9["AllocationThread"]
  cae90f9a_f201_a8e1_95cd_192dc599e5ef["PooledByteBufAllocatorTest.java"]
  6bb945aa_60fd_1a15_e603_8ad95417c6e9 -->|defined in| cae90f9a_f201_a8e1_95cd_192dc599e5ef
  46e263fa_fdc3_660c_4ebd_463688b0b22c["AllocationThread()"]
  6bb945aa_60fd_1a15_e603_8ad95417c6e9 -->|method| 46e263fa_fdc3_660c_4ebd_463688b0b22c
  cedeec8b_9344_3cef_05a1_e0c94d65820e["run()"]
  6bb945aa_60fd_1a15_e603_8ad95417c6e9 -->|method| cedeec8b_9344_3cef_05a1_e0c94d65820e
  75630b21_cf5b_0dec_6b14_64e015ce8b30["releaseBuffersAndCheckContent()"]
  6bb945aa_60fd_1a15_e603_8ad95417c6e9 -->|method| 75630b21_cf5b_0dec_6b14_64e015ce8b30
  0b4808a8_2897_6029_b5b6_96ac29b77599["isFinished()"]
  6bb945aa_60fd_1a15_e603_8ad95417c6e9 -->|method| 0b4808a8_2897_6029_b5b6_96ac29b77599
  ae2cbede_690d_b607_b7fd_b0b04f28d215["markAsFinished()"]
  6bb945aa_60fd_1a15_e603_8ad95417c6e9 -->|method| ae2cbede_690d_b607_b7fd_b0b04f28d215
  f49c8d97_9572_ea44_eea5_b4423389718a["joinAndCheckForError()"]
  6bb945aa_60fd_1a15_e603_8ad95417c6e9 -->|method| f49c8d97_9572_ea44_eea5_b4423389718a
  87eef12d_30b1_a2d7_ab7f_769716cfbc02["checkForError()"]
  6bb945aa_60fd_1a15_e603_8ad95417c6e9 -->|method| 87eef12d_30b1_a2d7_ab7f_769716cfbc02

Relationship Graph

Source Code

buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java lines 603–680

    private static final class AllocationThread extends Thread {

        private static final int[] ALLOCATION_SIZES = new int[16 * 1024];
        static {
            for (int i = 0; i < ALLOCATION_SIZES.length; i++) {
                ALLOCATION_SIZES[i] = i;
            }
        }

        private final Queue<ByteBuf> buffers = new ConcurrentLinkedQueue<ByteBuf>();
        private final ByteBufAllocator allocator;
        private final AtomicReference<Object> finish = new AtomicReference<Object>();

        AllocationThread(ByteBufAllocator allocator) {
            this.allocator = allocator;
        }

        @Override
        public void run() {
            try {
                int idx = 0;
                while (finish.get() == null) {
                    for (int i = 0; i < 10; i++) {
                        int len = ALLOCATION_SIZES[Math.abs(idx++ % ALLOCATION_SIZES.length)];
                        ByteBuf buf = allocator.directBuffer(len, Integer.MAX_VALUE);
                        assertEquals(len, buf.writableBytes());
                        while (buf.isWritable()) {
                            buf.writeByte(i);
                        }

                        buffers.offer(buf);
                    }
                    releaseBuffersAndCheckContent();
                }
            } catch (Throwable cause) {
                finish.set(cause);
            } finally {
                releaseBuffersAndCheckContent();
            }
        }

        private void releaseBuffersAndCheckContent() {
            int i = 0;
            while (!buffers.isEmpty()) {
                ByteBuf buf = buffers.poll();
                while (buf.isReadable()) {
                    assertEquals(i, buf.readByte());
                }
                buf.release();
                i++;
            }
        }

        boolean isFinished() {
            return finish.get() != null;
        }

        void markAsFinished() {
            finish.compareAndSet(null, Boolean.TRUE);
        }

        void joinAndCheckForError() throws Throwable {
            try {
                // Mark as finish if not already done but ensure we not override the previous set error.
                join();
            } finally {
                releaseBuffersAndCheckContent();
            }
            checkForError();
        }

        void checkForError() throws Throwable {
            Object obj = finish.get();
            if (obj instanceof Throwable) {
                throw (Throwable) obj;
            }
        }
    }

Frequently Asked Questions

What is the AllocationThread class?
AllocationThread is a class in the netty codebase, defined in buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java.
Where is AllocationThread defined?
AllocationThread is defined in buffer/src/test/java/io/netty/buffer/PooledByteBufAllocatorTest.java at line 603.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free