java.lang.ThreadLocal.get()方法的使用及代码示例

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

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

ThreadLocal.get介绍

[英]Returns the value of this variable for the current thread. If an entry doesn't yet exist for this variable on this thread, this method will create an entry, populating the value with the result of #initialValue().
[中]返回当前线程的此变量的值。如果此线程上还不存在此变量的条目,则此方法将创建一个条目,用#initialValue()的结果填充该值。

代码示例

代码示例来源: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: apache/incubator-dubbo

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

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

/**
 * Return the LocaleContext associated with the current thread, if any.
 * @return the current LocaleContext, or {@code null} if none
 */
@Nullable
public static LocaleContext getLocaleContext() {
  LocaleContext localeContext = localeContextHolder.get();
  if (localeContext == null) {
    localeContext = inheritableLocaleContextHolder.get();
  }
  return localeContext;
}

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

/**
 * Return the name of the currently proxied bean instance.
 * @return the name of the bean, or {@code null} if none available
 */
@Nullable
public static String getCurrentProxiedBeanName() {
  return currentProxiedBeanName.get();
}

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

/**
 * Return the JodaTimeContext associated with the current thread, if any.
 * @return the current JodaTimeContext, or {@code null} if none
 */
@Nullable
public static JodaTimeContext getJodaTimeContext() {
  return jodaTimeContextHolder.get();
}

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

/**
 * Return the {@link ResourceLoader} for the currently configured Quartz Scheduler,
 * to be used by {@link ResourceLoaderClassLoadHelper}.
 * <p>This instance will be set before initialization of the corresponding Scheduler,
 * and reset immediately afterwards. It is thus only available during configuration.
 * @see #setApplicationContext
 * @see ResourceLoaderClassLoadHelper
 */
@Nullable
public static ResourceLoader getConfigTimeResourceLoader() {
  return configTimeResourceLoaderHolder.get();
}

代码示例来源: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

/**
 * Return the name of the current transaction, or {@code null} if none set.
 * To be called by resource management code for optimizations per use case,
 * for example to optimize fetch strategies for specific named transactions.
 * @see org.springframework.transaction.TransactionDefinition#getName()
 */
@Nullable
public static String getCurrentTransactionName() {
  return currentTransactionName.get();
}

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

/**
 * Return the SimpAttributes currently bound to the thread.
 * @return the attributes or {@code null} if not bound
 */
@Nullable
public static SimpAttributes getAttributes() {
  return attributesHolder.get();
}

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

/**
 * Return the DateTimeContext associated with the current thread, if any.
 * @return the current DateTimeContext, or {@code null} if none
 */
@Nullable
public static DateTimeContext getDateTimeContext() {
  return dateTimeContextHolder.get();
}

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

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

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

/**
 * Return whether the specified prototype bean is currently in creation
 * (within the current thread).
 * @param beanName the name of the bean
 */
protected boolean isPrototypeCurrentlyInCreation(String beanName) {
  Object curVal = this.prototypesCurrentlyInCreation.get();
  return (curVal != null &&
      (curVal.equals(beanName) || (curVal instanceof Set && ((Set<?>) curVal).contains(beanName))));
}

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

@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
  Map<String, Object> scope = this.threadScope.get();
  Object scopedObject = scope.get(name);
  if (scopedObject == null) {
    scopedObject = objectFactory.getObject();
    scope.put(name, scopedObject);
  }
  return scopedObject;
}

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

private static int getNonGoldenRandomKey() {
  int key;
  do {
   key = random.get().nextInt();
  } while (key == GOLDEN_PRESENT_KEY);
  return key;
 }
}

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

@Override
 public void run() {
  threadLocalCount.set(threadLocalCount.get() + 1);
 }
};

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

@Override
 public Integer call() {
  int i = threadLocalCount.get();
  threadLocalCount.set(i + 1);
  return i;
 }
};

代码示例来源: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: google/guava

public final void tearDown() throws IOException {
  if (!fileThreadLocal.get().delete()) {
   logger.warning("Unable to delete file: " + fileThreadLocal.get());
  }
  fileThreadLocal.remove();
 }
}

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

/**
 * CycleDetectingLock implementations must call this method before attempting to acquire the lock.
 */
private void aboutToAcquire(CycleDetectingLock lock) {
 if (!lock.isAcquiredByCurrentThread()) {
  ArrayList<LockGraphNode> acquiredLockList = acquiredLocks.get();
  LockGraphNode node = lock.getLockGraphNode();
  node.checkAcquiredLocks(policy, acquiredLockList);
  acquiredLockList.add(node);
 }
}

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

@Override
 public void run() {
  try {
   Future<?> future = executor.submit(incrementTask);
   assertTrue(future.isDone());
   assertEquals(1, threadLocalCount.get().intValue());
  } catch (Throwable t) {
   throwableFromOtherThread.set(t);
  }
 }
});

相关文章