多线程调用指标

直接看代码:

 Java Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
List<ParamBean> paramBeanList =  new LinkedList<>();
//第一个指标开始:风控报表1-主要市场指数变化
paramBeanList.add( new ParamBean(endDate, requestParams( "RISKCONTROL_PRODUCT_INDEXQUOTE", endDate),   "RISKCONTROL_PRODUCT_INDEXQUOTE"));
//第二个指标开始:风控报表1-组合回撤止损检查
paramBeanList.add( new ParamBean(endDate, requestParams( "RISKCONTROL_PRODUCT_RETRACEMENT", fundType, endDate),   "RISKCONTROL_PRODUCT_RETRACEMENT"));
//第三个指标开始:风控报表-亏损超过20%个股-->没有发布
paramBeanList.add( new ParamBean(endDate, requestParams( "RISKCONTROL_PRODUCT_LOSSTHANTWENTY", fundType, endDate),   "RISKCONTROL_PRODUCT_LOSSTHANTWENTY"));
//第四个指标开始:风控报表-股票持仓及组合净资产
paramBeanList.add( new ParamBean(endDate, requestParams( "RISKCONTROL_PRODUCT_REALPOSITION", fundType, endDate),   "RISKCONTROL_PRODUCT_REALPOSITION"));
Map< String, ParamBean> paramMap =  new HashMap<>();
for (ParamBean paramBean : paramBeanList)
{
    paramMap.put(paramBean.getIndexName(), paramBean);
}

Map< String, Map< String, Object>> syncIndexCallback = syncIndexCallback(paramBeanList, paramMap);



/**
     * 开启多线程调用指标
     * @param paramBeanList
     * @param paramMap
     * @return
     */

private Map< String, Map< String, Object>> syncIndexCallback(List<ParamBean> paramBeanList, Map< String, ParamBean> paramMap)
{
    Map< String, Map< String, Object>> syncResultMap =  new HashMap<>();
     int taskSize =  4;
    ExecutorService pool = Executors.newFixedThreadPool(taskSize);
    List<Future<Map< String, Map< String, Object>>>> futureList =  new ArrayList<>();

    Callable<Map< String, Map< String, Object>>> call = null;
     // exe task
     for ( String indicatorName : paramMap.keySet())
    {
         final ParamBean paramBean = paramMap.get(indicatorName);
        call =  new Callable<Map< String, Map< String, Object>>>()
        {

            @Override
             public  Map< String, Map< String, Object>> call()  throws Exception
            {
                Map< String, Object> callIndex = callIndex(paramBean.getDate(), paramBean.getRequestParam(), paramBean.getIndexName());
                Map< String, Map< String, Object>> resultMap =  new HashMap<>();
                resultMap.put(paramBean.getIndexName(), callIndex);
                 return resultMap;
            }
        };
        futureList.add(pool.submit(call));
    }


     for( int k =  0; k < paramBeanList.size(); k++)
    {
        Future<Map< String, Map< String, Object>>> future = futureList.get(k);
         try
        {
            Boolean flag = future.isDone();
             while ((null == flag) || !flag)
            {
                Thread.sleep( 1000);
                flag = future.isDone();
            }
             if (null != future && future.isDone() && future.get() != null)
            {
                syncResultMap.putAll(future.get());
            }
        }
         catch (InterruptedException e)
        {
            e.printStackTrace();
        }
         catch (ExecutionException e)
        {
            e.printStackTrace();
        }
        future = null;
    }

    pool.shutdown();
    pool = null; // help GC
    futureList = null;
     return syncResultMap;
     //***multi thread over
}

class ParamBean
{
     private  String date ;
     private  String requestParam;
     private  String indexName;


     public  String getDate()
    {
         return date;
    }
     public  void setDate( String date)
    {
         this.date = date;
    }
     public  String getRequestParam()
    {
         return requestParam;
    }
     public  void setRequestParam( String requestParam)
    {
         this.requestParam = requestParam;
    }
     public  String getIndexName()
    {
         return indexName;
    }
     public  void setIndexName( String indexName)
    {
         this.indexName = indexName;
    }

     public ParamBean()
    {
    }
     public ParamBean( String date,  String requestParam,  String indexName)
    {
         this.date = date;
         this.requestParam = requestParam;
         this.indexName = indexName;
    }
    @Override
     public  String toString()
    {
         return  "ParamBean [date=" + date +  ", requestParam=" + requestParam +  ", indexName=" + indexName +  "]";
    }


}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值