java.lang.ThreadLocal类的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(6.2k)|赞(0)|评价(0)|浏览(133)

本文整理了Java中java.lang.ThreadLocal类的一些代码示例,展示了ThreadLocal类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThreadLocal类的具体详情如下:
包路径:java.lang.ThreadLocal
类名称:ThreadLocal

ThreadLocal介绍

[英]Implements a thread-local storage, that is, a variable for which each thread has its own value. All threads share the same ThreadLocal object, but each sees a different value when accessing it, and changes made by one thread do not affect the other threads. The implementation supports null values.
[中]实现线程本地存储,即每个线程都有自己的值的变量。所有线程共享同一个ThreadLocal对象,但每个线程在访问该对象时都会看到不同的值,并且一个线程所做的更改不会影响其他线程。该实现支持空值。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private void bindToThread() {
  // Expose current TransactionStatus, preserving any existing TransactionStatus
  // for restoration after this transaction is complete.
  this.oldTransactionInfo = transactionInfoHolder.get();
  transactionInfoHolder.set(this);
}

代码示例来源:origin: spring-projects/spring-framework

@Nullable
static TransactionContext removeCurrentTransactionContext() {
  TransactionContext transactionContext = currentTransactionContext.get();
  currentTransactionContext.remove();
  return transactionContext;
}

代码示例来源:origin: ch.qos.logback/logback-classic

/**
 * Clear all entries in the MDC.
 */
public void clear() {
  lastOperation.set(WRITE_OPERATION);
  copyOnThreadLocal.remove();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Used internally by CGLIB. Returns the <code>AbstractClassGenerator</code>
 * that is being used to generate a class in the current thread.
 */
public static AbstractClassGenerator getCurrent() {
  return (AbstractClassGenerator) CURRENT.get();
}

代码示例来源:origin: spring-projects/spring-framework

private void restoreThreadLocalStatus() {
  // Use stack to restore old transaction TransactionInfo.
  // Will be null if none was set.
  transactionInfoHolder.set(this.oldTransactionInfo);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Make the given proxy available via the {@code currentProxy()} method.
 * <p>Note that the caller should be careful to keep the old value as appropriate.
 * @param proxy the proxy to expose (or {@code null} to reset it)
 * @return the old proxy, which may be {@code null} if none was bound
 * @see #currentProxy()
 */
@Nullable
static Object setCurrentProxy(@Nullable Object proxy) {
  Object old = currentProxy.get();
  if (proxy != null) {
    currentProxy.set(proxy);
  }
  else {
    currentProxy.remove();
  }
  return old;
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Reset the LocaleContext for the current thread.
 */
public static void resetLocaleContext() {
  localeContextHolder.remove();
  inheritableLocaleContextHolder.remove();
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  public Kryo getKryo() {
    return holder.get();
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Set a ConnectionSpec for this proxy and the current thread.
 * The given ConnectionSpec will be applied to all subsequent
 * {@code getConnection()} calls on this ConnectionFactory proxy.
 * <p>This will override any statically specified "connectionSpec" property.
 * @param spec the ConnectionSpec to apply
 * @see #removeConnectionSpecFromCurrentThread
 */
public void setConnectionSpecForCurrentThread(ConnectionSpec spec) {
  this.threadBoundSpec.set(spec);
}

代码示例来源:origin: google/guava

@Override
void dispatch(Object event, Iterator<Subscriber> subscribers) {
 checkNotNull(event);
 checkNotNull(subscribers);
 Queue<Event> queueForThread = queue.get();
 queueForThread.offer(new Event(event, subscribers));
 if (!dispatching.get()) {
  dispatching.set(true);
  try {
   Event nextEvent;
   while ((nextEvent = queueForThread.poll()) != null) {
    while (nextEvent.subscribers.hasNext()) {
     nextEvent.subscribers.next().dispatchEvent(nextEvent.event);
    }
   }
  } finally {
   dispatching.remove();
   queue.remove();
  }
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Reset the JodaTimeContext for the current thread.
 */
public static void resetJodaTimeContext() {
  jodaTimeContextHolder.remove();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Set the name of the currently proxied bean instance.
 * @param beanName the name of the bean, or {@code null} to reset it
 */
static void setCurrentProxiedBeanName(@Nullable String beanName) {
  if (beanName != null) {
    currentProxiedBeanName.set(beanName);
  }
  else {
    currentProxiedBeanName.remove();
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Object invoke(MethodInvocation mi) throws Throwable {
  MethodInvocation oldInvocation = invocation.get();
  invocation.set(mi);
  try {
    return mi.proceed();
  }
  finally {
    invocation.set(oldInvocation);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return if transaction synchronization is active for the current thread.
 * Can be called before register to avoid unnecessary instance creation.
 * @see #registerSynchronization
 */
public static boolean isSynchronizationActive() {
  return (synchronizations.get() != null);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Expose a read-only flag for the current transaction.
 * Called by the transaction manager on transaction begin and on cleanup.
 * @param readOnly {@code true} to mark the current transaction
 * as read-only; {@code false} to reset such a read-only marker
 * @see org.springframework.transaction.TransactionDefinition#isReadOnly()
 */
public static void setCurrentTransactionReadOnly(boolean readOnly) {
  currentTransactionReadOnly.set(readOnly ? Boolean.TRUE : null);
}

代码示例来源:origin: google/guava

public final void tearDown() throws IOException {
  try {
   java.nio.file.Files.delete(fileThreadLocal.get());
  } catch (IOException e) {
   logger.log(Level.WARNING, "Unable to delete file: " + fileThreadLocal.get(), e);
  }
  fileThreadLocal.remove();
 }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Remove any user credentials for this proxy from the current thread.
 * Statically specified user credentials apply again afterwards.
 * @see #setCredentialsForCurrentThread
 */
public void removeCredentialsFromCurrentThread() {
  this.threadBoundCredentials.remove();
}

代码示例来源:origin: alibaba/fastjson

final char writeAfter(JSONSerializer serializer, Object object, char seperator) {
  serializerLocal.set(serializer);
  seperatorLocal.set(seperator);
  writeAfter(object);
  serializerLocal.set(null);
  return seperatorLocal.get();
}

代码示例来源:origin: google/guava

/** Returns a thread-local 1024-char array. */
static char[] charBufferFromThreadLocal() {
 return DEST_TL.get();
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Expose whether there currently is an actual transaction active.
 * Called by the transaction manager on transaction begin and on cleanup.
 * @param active {@code true} to mark the current thread as being associated
 * with an actual transaction; {@code false} to reset that marker
 */
public static void setActualTransactionActive(boolean active) {
  actualTransactionActive.set(active ? Boolean.TRUE : null);
}

相关文章