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

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

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

ThreadLocal.set介绍

[英]Sets the value of this variable for the current thread. If set to null, the value will be set to null and the underlying entry will still be present.
[中]为当前线程设置此变量的值。如果设置为null,则该值将设置为null,并且基础条目仍然存在。

代码示例

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

/**
 * 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: 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: 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);
}

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

@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

/**
 * Expose the name of the current transaction, if any.
 * Called by the transaction manager on transaction begin and on cleanup.
 * @param name the name of the transaction, or {@code null} to reset it
 * @see org.springframework.transaction.TransactionDefinition#getName()
 */
public static void setCurrentTransactionName(@Nullable String name) {
  currentTransactionName.set(name);
}

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

/**
 * Bind the given SimpAttributes to the current thread.
 * @param attributes the RequestAttributes to expose
 */
public static void setAttributes(@Nullable SimpAttributes attributes) {
  if (attributes != null) {
    attributesHolder.set(attributes);
  }
  else {
    resetAttributes();
  }
}

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

/**
 * Activate transaction synchronization for the current thread.
 * Called by a transaction manager on transaction begin.
 * @throws IllegalStateException if synchronization is already active
 */
public static void initSynchronization() throws IllegalStateException {
  if (isSynchronizationActive()) {
    throw new IllegalStateException("Cannot activate transaction synchronization - already active");
  }
  logger.trace("Initializing transaction synchronization");
  synchronizations.set(new LinkedHashSet<>());
}

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

private static InternalThreadLocalMap slowGet() {
  ThreadLocal<InternalThreadLocalMap> slowThreadLocalMap = InternalThreadLocalMap.slowThreadLocalMap;
  InternalThreadLocalMap ret = slowThreadLocalMap.get();
  if (ret == null) {
    ret = new InternalThreadLocalMap();
    slowThreadLocalMap.set(ret);
  }
  return ret;
}

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

private static InternalThreadLocalMap slowGet() {
  ThreadLocal<InternalThreadLocalMap> slowThreadLocalMap = InternalThreadLocalMap.slowThreadLocalMap;
  InternalThreadLocalMap ret = slowThreadLocalMap.get();
  if (ret == null) {
    ret = new InternalThreadLocalMap();
    slowThreadLocalMap.set(ret);
  }
  return ret;
}

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

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

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

protected Path createFile() throws IOException {
 Path file = java.nio.file.Files.createTempFile("SinkSourceFile", "txt");
 fileThreadLocal.set(file);
 return file;
}

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

protected File createFile() throws IOException {
 File file = File.createTempFile("SinkSourceFile", "txt");
 fileThreadLocal.set(file);
 return file;
}

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

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

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

@Before("serviceExecution()")
public void countUse() {
  this.useCount++;
  threadLocalCount.set(this.useCount);
}

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

private static Yylex getLexer(Reader reader) {
  Yylex ret = LOCAL_LEXER.get();
  if (ret == null) {
    ret = new Yylex(reader);
    LOCAL_LEXER.set(ret);
  } else {
    ret.yyreset(reader);
  }
  return ret;
}

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

private static Yylex getLexer(Reader reader) {
  Yylex ret = LOCAL_LEXER.get();
  if (ret == null) {
    ret = new Yylex(reader);
    LOCAL_LEXER.set(ret);
  } else {
    ret.yyreset(reader);
  }
  return ret;
}

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

@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
  String name = testContext.getTestMethod().getName();
  actualMethods.add(name);
  testContext.setAttribute("method", name);
  this.methodName.set(name);
}

相关文章