本文整理匯總了Java中org.springframework.core.NamedThreadLocal類的典型用法代碼示例。如果您正苦於以下問題:Java NamedThreadLocal類的具體用法?Java NamedThreadLocal怎麽用?Java NamedThreadLocal使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
NamedThreadLocal類屬於org.springframework.core包,在下文中一共展示了NamedThreadLocal類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: get
import org.springframework.core.NamedThreadLocal; //導入依賴的package包/類
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
try {
Object mutex = RequestContextHolder.currentRequestAttributes().getSessionMutex();
synchronized (mutex) {
return super.get(name, objectFactory);
}
} catch (IllegalStateException e) {
logger.debug("outside web session");
ThreadLocal<Object> objectHolder = objectHolders.get(name);
if (objectHolder == null) {
objectHolders.put(name, new NamedThreadLocal<>("wideScopeObjectHolder_" + name));
objectHolder = objectHolders.get(name);
}
if (objectHolder.get() == null) {
objectHolder.set(objectFactory.getObject());
}
return objectHolder.get();
}
}
示例2: test
import org.springframework.core.NamedThreadLocal; //導入依賴的package包/類
@Test
public void test(){
final ThreadLocal<Object> prototypesCurrentlyInCreation =
new NamedThreadLocal<Object>("Prototype beans currently in creation");
Assert.assertNull(prototypesCurrentlyInCreation.get());
prototypesCurrentlyInCreation.set("abc");
Assert.assertNotNull(prototypesCurrentlyInCreation.get());
prototypesCurrentlyInCreation.set(new HashSet<String>());
Assert.assertNotNull(prototypesCurrentlyInCreation.get());
prototypesCurrentlyInCreation.remove();
Assert.assertNull(prototypesCurrentlyInCreation.get());
System.out.println(prototypesCurrentlyInCreation.toString());
}