程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java完成的並發義務處置實例

Java完成的並發義務處置實例

編輯:關於JAVA

Java完成的並發義務處置實例。本站提示廣大學習愛好者:(Java完成的並發義務處置實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java完成的並發義務處置實例正文


本文實例講述了Java完成的並發義務處置辦法。分享給年夜家供年夜家參考,詳細以下:

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法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved