赞
踩
ThreadLocal 线程本地变量,每个线程有个一个该变量的副本,互相不影响,主要用于上下文共享,比如一个线程从控制层,输入到业务层,再到数据层,
该变量只是这个一个线程中使用
例子1:
public classContext {
privateString name;
privateString cardId;
public voidsetName(String name) {
this.name= name;}
publicString getName() {
returnname;}
public voidsetCardId(String cardId) {
this.cardId= cardId;}
publicString getCardId() {
returncardId;}
@OverridepublicString toString() {
return"Context{"+
"name='"+ name+ '\''+
", cardId='"+ cardId+ '\''+
'}';}
}
public classQueryAction {
public voidexecute(Context context){
try{
Thread.sleep(1000);String name = "alex"+Thread.currentThread().getName();context.setName(name);} catch(InterruptedException e) {
e.printStackTrace();}
}
}
public classQueryFromHttpAction {
public voidexecute(Context context) {
String name = context.getName();String cardId = getCardId(name);context.setCardId(cardId);}
privateString getCardId(String name) {
try{
Thread.sleep(1000);} catch(InterruptedException e) {
e.printStackTrace();}
return"3232332323323";}
}
public classExecutionTask implementsRunnable {
privateQueryAction queryAction= newQueryAction();privateQueryFromHttpAction queryFromHttpAction= newQueryFromHttpAction();
@Overridepublic voidrun() {
finalContext context = newContext();queryAction.execute(context);System.out.println(context.toString()+"");queryFromHttpAction.execute(context);System.out.println(context.toString()+"");}
}
importjava.util.stream.IntStream;
public classContextTest {
public static voidmain(String[] args) {
IntStream.range(1,5).forEach(i -> {
newThread(newExecutionTask()).start();});}
}
打印结果
Context{name='alexThread-1', cardId='null'}
Context{name='alexThread-3', cardId='null'}
Context{name='alexThread-2', cardId='null'}
Context{name='alexThread-0', cardId='null'}
Context{name='alexThread-3', cardId='3232332323323'}
Context{name='alexThread-1', cardId='3232332323323'}
Context{name='alexThread-0', cardId='3232332323323'}
Context{name='alexThread-2', cardId='3232332323323'}
上面例子1这是通过变量传递实现的,现在用ThreadLocal
例子2:
public classContext {
privateString name;
privateString cardId;
public voidsetName(String name) {
this.name= name;}
publicString getName() {
returnname;}
public voidsetCardId(String cardId) {
this.cardId= cardId;}
publicString getCardId() {
returncardId;}
@OverridepublicString toString() {
return"Context{"+
"name='"+ name+ '\''+
", cardId='"+ cardId+ '\''+
'}';}
}
public classActionContext {
private static finalThreadLocal threadLocal= newThreadLocal() {
@OverrideprotectedContext initialValue() {
return newContext();}
};
private static classContextHolder {
private final staticActionContext actionContext= newActionContext();}
public staticActionContext getActionContext() {
returnContextHolder.actionContext;}
public classExecutionTask implementsRunnable {
privateQueryAction queryAction= newQueryAction();privateQueryFromHttpAction queryFromHttpAction= newQueryFromHttpAction();
@Overridepublic voidrun() {
finalContext context = ActionContext.getActionContext().getContext();queryAction.execute();System.out.println(context.toString()+"");queryFromHttpAction.execute();System.out.println(context.toString()+"");}
}
public classQueryAction {
public voidexecute(){
try{
Context context = ActionContext.getActionContext().getContext();Thread.sleep(1000);String name = "alex"+Thread.currentThread().getName();context.setName(name);} catch(InterruptedException e) {
e.printStackTrace();}
}
}
public classQueryFromHttpAction {
public voidexecute() {
Context context = ActionContext.getActionContext().getContext();String name = context.getName();String cardId = getCardId(name);context.setCardId(cardId);}
privateString getCardId(String name) {
try{
Thread.sleep(1000);} catch(InterruptedException e) {
e.printStackTrace();}
return"3232332323323";}
}
public classContextTest {
public static voidmain(String[] args) {
IntStream.range(1,5).forEach(i -> {
newThread(newExecutionTask()).start();});}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。