Double the thread local buffer capacity. - Java java.nio

Java examples for java.nio:DoubleBuffer

Description

Double the thread local buffer capacity.

Demo Code

/*/*from w  w w.ja  v  a  2s .c  om*/
Pulsar
Copyright (C) 2013-2015 eBay Software Foundation
Licensed under the GPL v2 license.  See LICENSE for full terms.
 */
//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(enlargeThreadLocalByteBuffer());
    }

    private static final ThreadLocal<ByteBuffer> TMP_BUFFER = new ThreadLocal<ByteBuffer>() {
        @Override
        protected ByteBuffer initialValue() {
            return ByteBuffer.allocate(4096);
        }
    };

    /**
     * Double the thread local buffer capacity.
     * 
     * @return
     */
    public static ByteBuffer enlargeThreadLocalByteBuffer() {
        TMP_BUFFER
                .set(ByteBuffer.allocate(TMP_BUFFER.get().capacity() * 2));
        return TMP_BUFFER.get();
    }
}

Related Tutorials