赞
踩
后一个方法可能需要用到前一个阶段的结果
单例的
/** * 上下文 */ public class Context { private String name; private String cardId; public void setName(String name) { this.name = name; } public String getName() { return name; } public void setCardId(String cardId) { this.cardId = cardId; } public String getCardId() { return cardId; } }
/**
* 模拟从数据库拿数据
*/
public class QueryFromDBAction {
public void excecute(Context context){
try {
Thread.sleep(1000);
String name = "Alex" + Thread.currentThread().getName();
context.setName(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/** * 获取身份证号 */ public class QueryFromHttpAction { public void excecute(Context context){ String name = context.getName(); String cardId = getCardId(name); context.setCardId(cardId); } private String getCardId(String name){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "146433131" + Thread.currentThread().getId(); } }
public class ExecutionTask implements Runnable {
private QueryFromDBAction queryAction = new QueryFromDBAction();
private QueryFromHttpAction httpAction = new QueryFromHttpAction();
@Override
public void run() {
final Context context =new Context();
queryAction.excecute(context);
httpAction.excecute(context);
System.out.println("用户的名字为"+ context.getName() + " ,身份证号是"+context.getCardId());
}
}
public class ContextTest {
public static void main(String[] args) {
IntStream.range(1,5).forEach(i->{
new Thread(new ExecutionTask()).start();
});
}
}
基本思路是:
线程隔离的,不会被其他线程修改
,所以是线程安全的
设计一个单例,里面保存一个唯一的ThreadLocal
设计一个默认值
,在第一次获取时,默认构造一个context供外界修改public class ActionContext { private static final ThreadLocal<Context> THREAD_LOCAL = new ThreadLocal<Context>(){ @Override protected Context initialValue() { return new Context(); } }; private static class ContextHolder{ private final static ActionContext ACTION_CONTEXT = new ActionContext(); } public static ActionContext getInstance(){ return ContextHolder.ACTION_CONTEXT; } public Context getContext(){ return THREAD_LOCAL.get(); } }
/** * 获取身份证号 */ public class QueryFromHttpAction { public void excecute(){ Context context = ActionContext.getInstance().getContext(); String cardId = getCardId(context.getName()); context.setCardId(cardId); } private String getCardId(String name){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "146433131" + Thread.currentThread().getId(); } }
/**
* 模拟从数据库拿数据
*/
public class QueryFromDBAction {
public void excecute( ){
try {
Thread.sleep(1000);
String name = "Alex" + Thread.currentThread().getName();
ActionContext.getInstance().getContext().setName(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ExecutionTask implements Runnable {
private QueryFromDBAction queryAction = new QueryFromDBAction();
private QueryFromHttpAction httpAction = new QueryFromHttpAction();
@Override
public void run() {
queryAction.excecute();
httpAction.excecute();
Context context =ActionContext.getInstance().getContext();
System.out.println("用户的名字为"+ context.getName() + " ,身份证号是"+context.getCardId());
}
}
注意:线程池中如果使用该设计模式,需要先清理掉上一次执行的Context
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。