Java實現(xiàn)的并發(fā)任務處理實例
本文實例講述了Java實現(xiàn)的并發(fā)任務處理方法。分享給大家供大家參考,具體如下:
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("執(zhí)行IO異常", e);
throw e;
}
}
});
t.setDaemon(true);
t.setName("FootballService-IO-" + id.getAndIncrement());
return t;
}
});
}
//從fdate到tdate, 結果: 日期 30個區(qū)的創(chuàng)建角色總和
public Map<String, Long> countCreateRole(String fdate, String tdate, final String channel, Map<String, Object> gameConfig) throws Exception {
// 只讀數(shù)據(jù)不需要處理并發(fā)
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程序設計有所幫助。
相關文章
java 獲取HttpRequest Header的幾種方法(必看篇)
下面小編就為大家?guī)硪黄猨ava 獲取HttpRequest Header的幾種方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09
java實現(xiàn)優(yōu)酷視頻地址解析示例代碼分享
最近做了一個在線視頻的下載器,需要解析youku的視頻,獲得真正的視頻地址,現(xiàn)在把解析過程記錄下來以供參考2014-01-01
Java調(diào)用opencv實現(xiàn)圖片矯正功能
這篇文章主要為大家詳細介紹了Java如何調(diào)用opencv實現(xiàn)圖片矯正功能,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學習一下2023-09-09
詳解MyBatis Mapper 代理實現(xiàn)數(shù)據(jù)庫調(diào)用原理
這篇文章主要介紹了詳解MyBatis Mapper 代理實現(xiàn)數(shù)據(jù)庫調(diào)用原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
使用AbstractRoutingDataSource實現(xiàn)數(shù)據(jù)源動態(tài)切換的實例
AbstractRoutingDataSource 是 Spring 框架提供的一個抽象類,用于實現(xiàn)動態(tài)數(shù)據(jù)源路由,這個類主要用于多數(shù)據(jù)源場景,其中可以根據(jù)不同的條件動態(tài)地切換到不同的數(shù)據(jù)源,本文給大家介紹了如何使用AbstractRoutingDataSource實現(xiàn)數(shù)據(jù)源動態(tài)切換,需要的朋友可以參考下2024-03-03
Spring Boot 項目啟動自動執(zhí)行方法的兩種實現(xiàn)方式
這篇文章主要介紹了Spring Boot 項目啟動自動執(zhí)行方法的兩種實現(xiàn)方式,幫助大家更好的理解和學習使用Spring Boot框架,感興趣的朋友可以了解下2021-05-05
spring security集成cas實現(xiàn)單點登錄過程
這篇文章主要介紹了spring security集成cas實現(xiàn)單點登錄過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

