当前位置:   article > 正文

java线程上下文_ThreadLocal线程上下文使用

java中怎么在线程池中使用同一个上下文

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();});}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/949020
推荐阅读
相关标签
  

闽ICP备14008679号