本文实例讲述了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. JAVA 多线程和并发学习笔记(三)

    Java并发编程中使用Executors类创建和管理线程的用法 1.类 Executors Executors类可以看做一个“工具类”.援引JDK1.6 API中的介绍: 此包中所定义的 Execut ...

  2. JAVA多线程和并发基础面试问答(转载)

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  3. [转] JAVA多线程和并发基础面试问答

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  4. JAVA多线程和并发基础面试问答

    转载: JAVA多线程和并发基础面试问答 多线程和并发问题是Java技术面试中面试官比较喜欢问的问题之一.在这里,从面试的角度列出了大部分重要的问题,但是你仍然应该牢固的掌握Java多线程基础知识来对 ...

  5. 【多线程】JAVA多线程和并发基础面试问答(转载)

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  6. java处理高并发高负载类网站的优化方法

    java处理高并发高负载类网站中数据库的设计方法(java教程,java处理大量数据,java高负载数据) 一:高并发高负载类网站关注点之数据库 没错,首先是数据库,这是大多数应用所面临的首个SPOF ...

  7. (转)JAVA多线程和并发基础面试问答

    JAVA多线程和并发基础面试问答 原文链接:http://ifeve.com/java-multi-threading-concurrency-interview-questions-with-ans ...

  8. Java多线程与并发模型之锁

    这是一篇总结Java多线程开发的长文.文章是从Java创建之初就存在的synchronized关键字引入,对Java多线程和并发模型进行了探讨.希望通过此篇内容的解读能帮助Java开发者更好的理清Ja ...

  9. Java编程思想 - 并发

    前言 Q: 为什么学习并发? A: 到目前为止,你学到的都是有关顺序编程的知识,即程序中的所有事物在任意时刻都只能执行一个步骤. A: 编程问题中相当大的一部分都可以通过使用顺序编程来解决,然而,对于 ...

随机推荐

  1. bootstrap课程10 从外部引入视频到页面用什么标签

    bootstrap课程10 从外部引入视频到页面用什么标签 一.总结 一句话总结:a.iframe标签:b.embed标签:c.video标签 1.bootstrap具有响应式特性的嵌入内容如何实现? ...

  2. R语言-方差分析

    方差分析指的是不同变量之间互相影响从而导致结果的变化 1.单因素方差分析: 案例:50名患者接受降低胆固醇治疗的药物,其中三种治疗条件使用药物相同(20mg一天一次,10mg一天两次,5mg一天四次) ...

  3. Hypervisor, computer system, and virtual processor scheduling method

    A hypervisor calculates the total number of processor cycles (the number of processor cycles of one ...

  4. COGS——C 908. 校园网 || 洛谷——P 2746 [USACO5.3]校园网Network of Schools

    http://www.cogs.pro/cogs/problem/problem.php?pid=908   ||  https://www.luogu.org/problem/show?pid=27 ...

  5. Android中Application类的详解:

    Android中Application类的详解: 我们在平时的开发中,有时候可能会须要一些全局数据.来让应用中的全部Activity和View都能訪问到.大家在遇到这样的情况时,可能首先会想到自定义一 ...

  6. [D3] Add label text

    If we want to add text to a node or a image // Create container for the images const svgNodes = svg ...

  7. 15.SpringBoot简介-SpringBoot是什么可以做什么

    转自:https://blog.csdn.net/kingboyworld/article/details/77713743 在过去的两年时间里,最让人兴奋.回头率最高.最能改变游戏规则的东西,大概就 ...

  8. GO语言学习(六)Go 语言数据类型

    在 Go 编程语言中,数据类型用于声明函数和变量. 数据类型的出现是为了把数据分成所需内存大小不同的数据,编程的时候需要用大数据的时候才需要申请大内存,就可以充分利用内存. Go 语言按类别有以下几种 ...

  9. P2P平台很赚钱么?

    最近几年,搞P2P网贷和财富投资相关的金融周边公司,多了很多,楼下门店和电梯里的贷款小广告,真是多啊. 大家都去搞一件事的时候,很可能是大家都觉得这件事有利可图.但事实是,赚钱的总是少数,看到别人搞的 ...

  10. [React Intl] Format Date and Time Using react-intl FormattedDate and FormattedTime

    Using the react-intl FormattedDate and FormattedTime components, we’ll render a JavaScript Date into ...