Java实现的并发任务处理实例
程序员文章站
2024-03-08 09:23:40
本文实例讲述了java实现的并发任务处理方法。分享给大家供大家参考,具体如下:
public void init() {
super.init();
th...
本文实例讲述了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程序设计有所帮助。
上一篇: DAM 简单跨数据库ADO.NET组件