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();
}
본고에서 기술한 것이 여러분의 자바 프로그램 설계에 도움이 되기를 바랍니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.