/**
* 线程运行上下文设计模式
* 上下文
*/
@Data
public class Context {
private String name;
private String cardId;
}
/**
* 线程运行上下文设计模式
* 从数据库查询数据
*/
public class QueryFromDbAction {
public void execute(Context context) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
String name = "Alex-" + Thread.currentThread().getName();
context.setName(name);
}
}
/**
* 线程运行上下文设计模式
* 调用接口获取数据
*/
public class QueryFromHttpAction {
public void execute(Context context) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
String name = context.getName();
String cardId = getCardId(name);
context.setCardId(cardId);
}
private String getCardId(String name) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "510823" + Thread.currentThread().getId();
}
}
/**
* 线程运行上下文设计模式
* 线程执行任务
*/
public class ExecutionTask implements Runnable {
private QueryFromDbAction queryAction = new QueryFromDbAction();
private QueryFromHttpAction httpAction = new QueryFromHttpAction();
@Override
public void run() {
Context context = new Context();
queryAction.execute(context);
System.out.println("The name query successful.");
httpAction.execute(context);
System.out.println("The card id query successful.");
System.out.println("The Name is " + context.getName() + " and CardId is " + context.getCardId());
System.out.println(context);
}
}
/**
* 线程运行上下文设计模式
* 上下文测试
*/
public class ContextTest {
public static void main(String[] args) {
IntStream.rangeClosed(1, 4).forEach(i -> {
new Thread(new ExecutionTask(), "T" + i).start();
});
}
}
通过ThreadLocal,不用传Context对象
/**
* 线程运行上下文设计模式
* 线程单例上下文
*/
public class ActionContext {
private ActionContext() {
}
private static class ContextHolder {
private static final ActionContext actionContext = new ActionContext();
}
public static ActionContext getInstance() {
return ContextHolder.actionContext;
}
private static final ThreadLocal<Context> threadLocal = ThreadLocal.withInitial(Context::new);
public Context getContext() {
return threadLocal.get();
}
}
/**
* 线程运行上下文设计模式
* 上下文
*/
@Data
public class Context {
private String name;
private String cardId;
}
/**
* 线程运行上下文设计模式
* 线程执行任务
*
*/
public class ExecutionTask implements Runnable {
private QueryFromDbAction queryAction = new QueryFromDbAction();
private QueryFromHttpAction httpAction = new QueryFromHttpAction();
@Override
public void run() {
queryAction.execute();
System.out.println("The name query successful.");
httpAction.execute();
System.out.println("The card id query successful.");
ActionContext actionContext = ActionContext.getInstance();
Context context = actionContext.getContext();
System.out.println("The Name is " + context.getName() + " and CardId is " + context.getCardId());
System.out.println(context);
}
}
/**
* 线程运行上下文设计模式
* 从数据库查询数据
*
*/
public class QueryFromDbAction {
public void execute() {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
String name = "Alex-" + Thread.currentThread().getName();
ActionContext actionContext = ActionContext.getInstance();
Context context = actionContext.getContext();
context.setName(name);
}
}
/**
* 线程运行上下文设计模式
* 调用接口获取数据
*
*/
public class QueryFromHttpAction {
public void execute() {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
ActionContext actionContext = ActionContext.getInstance();
Context context = actionContext.getContext();
String name = context.getName();
String cardId = getCardId(name);
context.setCardId(cardId);
}
private String getCardId(String name) {
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "510823" + Thread.currentThread().getId();
}
}
/**
* 线程运行上下文设计模式
* 上下文测试
*
*/
public class ContextTest {
public static void main(String[] args) {
IntStream.rangeClosed(1, 4).forEach(i -> {
new Thread(new ExecutionTask(), "T" + i).start();
});
}
}