一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java實現的并發任務處理實例

Java實現的并發任務處理實例

2020-03-03 18:41xiaofancn JAVA教程

這篇文章主要介紹了Java實現的并發任務處理方法,結合實例形式較為詳細的分析了基于線程操作并發任務的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品久久久精品视频 | 99热这里有免费国产精品 | 亚洲精品免费在线观看 | 91久久精品国产一区二区 | 久热这里在线精品 | www四虎影院| 校花被老头夺去第一次动图 | 青青久在线视频免费观看 | 青青成人在线 | 美女福利视频午夜在线 | 久久99热在线观看7 久久99精品涩AV毛片观看 | 亚久久伊人精品青青草原2020 | 日韩av.com | 狠狠插综合网 | 手机看片福利 | 天使萌痴汉在线中文字幕 | uoco福利姬网站 | 国产午夜成人无码免费看 | 99精品免费在线 | 久久久久伊人 | 日本人成年视频在线观看 | 大香线一本 | 茄子视频懂你更多apl | 99热这里有免费国产精品 | 秋霞一级黄色片 | 欧美草逼网| 女人叉开腿让男人桶 | 亚洲mm色国产网站 | 日韩精品成人免费观看 | 女人国产香蕉久久精品 | 日韩成人在线视频 | 日日网| 青青草高清视频 | 白丝爆动漫羞羞动漫网站 | 97影院伦理| a黄毛片 | 亚洲午夜精品久久久久久抢 | 好大好爽好硬我要喷水了 | 99久久久久久久 | 男人视频网 | 深夜草莓视频 |