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();

	}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值