服务器之家

服务器之家 > 正文

Java实现的并发任务处理实例

时间:2020-03-03 18:41     来源/作者:xiaofancn

本文实例讲述了Java实现的并发任务处理方法。分享给大家供大家参考,具体如下:

?
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
public void init() {
 super.init();
 this.ioThreadPool = new ThreadPoolExecutor(50, 50, Long.MAX_VALUE, TimeUnit.SECONDS, new java.util.concurrent.LinkedTransferQueue<Runnable>(), new ThreadFactory() {
  AtomicLong id = new AtomicLong();
  @Override
  public Thread newThread(final Runnable r) {
   Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
     try {
      r.run();
     } catch (RuntimeException e) {
      logger.error("执行IO异常", e);
      throw e;
     }
    }
   });
   t.setDaemon(true);
   t.setName("FootballService-IO-" + id.getAndIncrement());
   return t;
  }
 });
}
//从fdate到tdate, 结果: 日期 30个区的创建角色总和
public Map<String, Long> countCreateRole(String fdate, String tdate, final String channel, Map<String, Object> gameConfig) throws Exception {
 // 只读数据不需要处理并发
 final Map<String, String> param = new HashMap<String, String>();
 param.put("fdate", fdate);
 param.put("tdate", tdate);
 param.put("channel", channel);
 final Map<String, Long> date_count = new HashMap<String, Long>();
 final List<String> zones = (List<String>) gameConfig.get("areas");
 Set<String> dateSet = JdbcTool.getDateRangeByDay(fdate, tdate, "yyyy-MM-dd");
 List<Future<Void>> tasks = new ArrayList<>(zones.size());
 for (String date : dateSet) {
  final String _date = date;
  tasks.add(publicThread.submit(new Callable<Void>() {
   @Override
   public Void call() {
    final AtomicLong count = new AtomicLong();
    List<Future<Void>> subTasks = new ArrayList<>(zones.size());
    for (String _zone : zones) {
     final String zone = _zone;
     subTasks.add(ioThreadPool.submit(new Callable<Void>() {
      @Override
      public Void call() throws Exception {
       JdbcTemplate _jdbcTemplate = dataSourceManager.getJdbcTemplate(zone);
       String database = dataSourceManager.getDatabase(zone);
       String _count = mget(CacheConstant.RZRoleCreateCount, zone + "#" + _date + "#" + channel + "#");
       if (_count == null) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT count(roleId) as count ");
        sb.append("from " + database + "_log.role ");
        sb.append("WHERE DATE(createTime)='" + _date + "' ");
        if (param.get("channel") != null) {
         sb.append(" AND channelId = '" + channel + "' ");
        }
        long queryForLong = _jdbcTemplate.queryForLong(sb.toString());
        count.addAndGet(queryForLong);
        mput(CacheConstant.RZRoleCreateCount, zone + "#" + _date + "#" + channel + "#", queryForLong + "");
       } else {
        count.addAndGet(Long.valueOf(_count));
       }
       return null;
      }
     }));
    }
    for (Future<Void> task : subTasks) {
     try {
      task.get();
     } catch (Exception e) {
      throw new RuntimeException(e);
     }
    }
    synchronized (date_count) {
     date_count.put(_date, count.get());
    }
    return null;
   }
  }));
 }
 for (Future<Void> task : tasks) {
  task.get();
 }
 return date_count;
}
@PreDestroy
public void destroy() {
 this.ioThreadPool.shutdownNow();
}

希望本文所述对大家java程序设计有所帮助。

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意
沙雕群名称大全2019精选 今年最火的微信群名沙雕有创意 2019-07-07
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
返回顶部