zoukankan      html  css  js  c++  java
  • 线程上下文

    //设计为单例
    public final class ActionContext {
    
        // 构造方法私有化
        private ActionContext() {
    
        }
    
        // Holder类
        private static class ContextHolder {
            private final static ActionContext actionContext = new ActionContext();
    
        }
    
        // 提供给外部使用
        public static ActionContext getActionContext() {
            return ContextHolder.actionContext;
        }
    
        // 属性
        public Context getContext() {
            return threadLocal.get();
        }
    
        // 属性
        private final ThreadLocal<Context> threadLocal = new ThreadLocal<Context>() {
            @Override
            protected Context initialValue() {
                return new Context();
            }
        };
    
    }
    
    
    public class Context {
    
        private String name;
        private String cardId;
    
        public String getCardId() {
            return cardId;
        }
    
        public void setCardId(String cardId) {
            this.cardId = cardId;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
    
    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 cardId query successful");
    
            Context context = ActionContext.getActionContext().getContext();
            System.out.println("The Name is " + context.getName() + " and CardId  is " + context.getCardId());
        }
    }
    
    public class QueryFromDBAction {
    
        public void execute() {
    
            try {
                Thread.sleep(1000L);
                String name = Thread.currentThread().getName() +"中的Jack";
                ActionContext.getActionContext().getContext().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(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return Thread.currentThread().getName()+"中的cardId";
        }
    }
    
    public class ContextTest {
    
        public static void main(String[] args) {
    
            IntStream.rangeClosed(1, 4)
                    .forEach(i ->
                            new Thread(new ExecutionTask(),"【线程"+i+"").start()
                    );
        }
    }
  • 相关阅读:
    2020Python作业——类与对象
    图文存储常识:单机、集中、分布式、云、云原生存储
    宜泊科技加入阿里云原生合作伙伴计划,共建智慧停车新生态
    dubbogo 3.0:牵手 gRPC 走向云原生时代
    千万商家的智能决策引擎AnalyticDB如何助力生意参谋双十一
    我在阿里云做前端代码智能化
    网络病毒源的排查(2005年3月22日维护记录)
    在页面中控制媒体流的起播点和播放长度
    注意服务器系统日期对防病毒软件的影响
    修改SQL SERVER虚拟服务器IP的问题
  • 原文地址:https://www.cnblogs.com/moris5013/p/11779477.html
Copyright © 2011-2022 走看看