ThreadLocal线程上下文使用
该变量只是这个一个线程中使用
例子1:
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;
}
@Override
public String toString() {
return "Context{" +
"name="" + name + """ +
", cardId="" + cardId + """ +
"}";
}
}
public class QueryAction {public void execute(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 execute(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 "3232332323323";
}
}
public class ExecutionTask implements Runnable {private QueryAction queryAction = new QueryAction();
private QueryFromHttpAction queryFromHttpAction = new QueryFromHttpAction();
@Override
public void run() {
final Context context = new Context();
queryAction.execute(context);
System.out.println(context.toString()+"");
queryFromHttpAction.execute(context);
System.out.println(context.toString()+"");
}
}
import java.util.stream.IntStream;public class ContextTest {
public static void main(String[] args) {
IntStream.range(1, 5).forEach(i -> {
new Thread(new ExecutionTask()).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 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;
}
@Override
public String toString() {
return "Context{" +
"name="" + name + """ +
", cardId="" + cardId + """ +
"}";
}
}
public class ActionContext {private static final ThreadLocal<Context> threadLocal = new ThreadLocal<Context>() {
@Override
protected Context initialValue() {
return new Context();
}
};
private static class ContextHolder {
private final static ActionContext actionContext = new ActionContext();
}
public static ActionContext getActionContext() {
return ContextHolder.actionContext;
}
public class ExecutionTask implements Runnable {private QueryAction queryAction = new QueryAction();
private QueryFromHttpAction queryFromHttpAction = new QueryFromHttpAction();
@Override
public void run() {
final Context context = ActionContext.getActionContext().getContext();
queryAction.execute();
System.out.println(context.toString()+"");
queryFromHttpAction.execute();
System.out.println(context.toString()+"");
}
}
public class QueryAction {public void execute(){
try {
Context context = ActionContext.getActionContext().getContext();
Thread.sleep(1000);
String name = "alex"+Thread.currentThread().getName();
context.setName(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class QueryFromHttpAction {public void execute() {
Context context = ActionContext.getActionContext().getContext();
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 "3232332323323";
}
}
public class ContextTest {public static void main(String[] args) {
IntStream.range(1, 5).forEach(i -> {
new Thread(new ExecutionTask()).start();
});
}
}
以上是 ThreadLocal线程上下文使用 的全部内容, 来源链接: utcz.com/z/513718.html