Example usage for java.lang ThreadLocal ThreadLocal

List of usage examples for java.lang ThreadLocal ThreadLocal

Introduction

In this page you can find the example usage for java.lang ThreadLocal ThreadLocal.

Prototype

public ThreadLocal() 

Source Link

Document

Creates a thread local variable.

Usage

From source file:ReadWriteLock.java

/**
 * Constructs read-write lock.//from ww  w . j a  va2 s  .  c o m
 */

public ReadWriteLock() {
    readLocksByThread = new ThreadLocal() {
        public Object initialValue() {
            return new Count();
        }
    };

    readLocks = 0;
    writeLocks = 0;
    writeLockedBy = null;
}

From source file:org.talend.dataquality.common.inference.ConcurrentAnalyzer.java

private ConcurrentAnalyzer(int maxSize, AnalyzerSupplier<Analyzer<T>> supplier) {
    GenericKeyedObjectPool.Config config = new GenericKeyedObjectPool.Config();
    config.maxTotal = maxSize;/*from  www .  j a  v  a  2 s .c om*/
    config.maxActive = maxSize;
    config.maxIdle = maxSize / 2;
    config.minIdle = maxSize / 2;
    config.maxWait = -1;
    // #1: Initialize a ThreadLocal backed with a generic object pool -> allows getting previously borrowed instance
    // and return to pool on remove() call.
    // #2: Pool is expected to be thread safe.
    final KeyedObjectPool<Thread, Analyzer<T>> pool = new GenericKeyedObjectPool<>(new Factory<>(supplier),
            config);
    this.threadLocal = new ThreadLocal<Analyzer<T>>() {

        @Override
        protected Analyzer<T> initialValue() {
            try {
                return pool.borrowObject(Thread.currentThread());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void remove() {
            try {
                // Order matters here as remove() causes get() to return null.
                pool.returnObject(Thread.currentThread(), get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                // Thread local keeps a lot of references, make sure everything gets cleaned up on error.
                super.remove();
            }
        }
    };
}

From source file:de.scoopgmbh.copper.persistent.StandardJavaSerializer.java

public void setCompressorMaxSize(int compressorMaxSize) {
    this.compressorMaxSize = compressorMaxSize;
    compressorTL = new ThreadLocal<Compressor>() {
        @Override/*from w  w w  .j a  v a 2s .co m*/
        protected Compressor initialValue() {
            return new Compressor(Deflater.BEST_COMPRESSION, StandardJavaSerializer.this.compressorMaxSize);
        }
    };
}

From source file:com.anite.meercat.PersistenceLocator.java

/**
 * Private Constructor to prevent construction
 *//*from   w w  w.java  2  s .  c  o  m*/
private PersistenceLocator() {
    if (threadLocal == null) {
        threadLocal = new ThreadLocal();
    }
}

From source file:de.scoopgmbh.copper.test.versioning.compatibility.TestJavaSerializer.java

public void setCompressorMaxSize(int compressorMaxSize) {
    this.compressorMaxSize = compressorMaxSize;
    compressorTL = new ThreadLocal<Compressor>() {
        @Override/*from   ww w.j  a  v a2 s .c om*/
        protected Compressor initialValue() {
            return new Compressor(Deflater.BEST_COMPRESSION, TestJavaSerializer.this.compressorMaxSize);
        }
    };
}

From source file:org.dragoneronca.nlp.wol.domain.WolDomainContext.java

private WolDomainContext() {
    WolConfiguration wolConfiguration = WolConfiguration.getInstance();
    PropertiesConfiguration configuration = wolConfiguration.getConfiguration(CONFIG_FILE);
    String persistenceUnitName = configuration.getString(PERSISTENCE_UNIT_NAME);
    entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
    tlEntityManager = new ThreadLocal<>();
}

From source file:monasca.thresh.infrastructure.persistence.AlarmDAOImpl.java

@Inject
public AlarmDAOImpl(DBI db) {
    this.db = db;
    this.simpleDateFormatter = new ThreadLocal<>();
}

From source file:com.workplacesystems.queuj.process.seam.SeamTransaction.java

@Factory(value = "transactionContext", autoCreate = true, scope = ScopeType.APPLICATION)
public ThreadLocal<TransactionContext> createTransactionContext() {
    return new ThreadLocal<TransactionContext>() {

        @Override/*from   ww w . j  a va 2s  . com*/
        protected TransactionContext initialValue() {
            TransactionContext context = new TransactionContext();
            log.debug("Starting transaction " + context.transactionId);
            return context;
        }
    };
}

From source file:com.stratio.cassandra.index.schema.ColumnMapperDate.java

/**
 * Builds a new {@link ColumnMapperDate} using the specified pattern.
 *
 * @param pattern The {@link SimpleDateFormat} pattern to be used.
 *//*w w w.  j a  v  a  2  s .c  o  m*/
@JsonCreator
public ColumnMapperDate(@JsonProperty("pattern") String pattern) {
    super(new AbstractType<?>[] { AsciiType.instance, UTF8Type.instance, Int32Type.instance, LongType.instance,
            IntegerType.instance, FloatType.instance, DoubleType.instance, DecimalType.instance,
            TimestampType.instance }, new AbstractType[] { LongType.instance, TimestampType.instance });
    this.pattern = pattern == null ? DEFAULT_PATTERN : pattern;
    concurrentDateFormat = new ThreadLocal<DateFormat>() {
        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat(ColumnMapperDate.this.pattern);
        }
    };
}

From source file:org.apache.lens.cube.metadata.UpdatePeriod.java

private static DateFormat getSecondlyFormat() {
    if (secondlyFormat == null) {
        secondlyFormat = new ThreadLocal<DateFormat>() {
            @Override/* w w  w .  j a  va2  s  . com*/
            protected SimpleDateFormat initialValue() {
                return new SimpleDateFormat(SECONDLY.formatStr());
            }
        };
    }
    return secondlyFormat.get();
}