java多线程:上下文切换设计模式

/**
 * 线程运行上下文设计模式
 * 上下文
 */
@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();
        });
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值