Java實現的並發任務處理執行個體_java

來源:互聯網
上載者:User

本文執行個體講述了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;}@PreDestroypublic void destroy() { this.ioThreadPool.shutdownNow();}

希望本文所述對大家java程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.