Thread - 多线程更新(update)数据库表的例子

目标:为提高数据库表更新(update)效率,使用多线程更新。其实这里也可以考虑另一种方法批量更新,不过如果更新失败了,同一事务(transaction)中的其他更新语句就会回滚,比较麻烦,所在还是简单点用多线程去处理。


困难点:开始是想把需要更新的数据等分到线程中去处理,不过搞了一段时间都没成功,主要是没有什么办法把动态参数从线程外传进线程内。后来换了个思路,每个线程去拿一条数据去更新,拿数据时同步处理。这样之后就可以了。

例子(部分外部类没有写上去,所以只能参考,要运行需要改改):

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class BaiduBaikeJob {
	static final Logger logger = LoggerFactory
			.getLogger(BaiduBaikeJob.class);
	@Autowired
	private MongoLockService mongoLockService;
	@Autowired
	private WeiboPersonService weiboPersonService;
	@Resource(name = "globalProperties")
	private PropertyPlaceholderConfigurer config;
	
	final int THREAD_COUNT = 10;
	AtomicInteger updCounter = new AtomicInteger(0);
	AtomicInteger next = new AtomicInteger(0);
	private CountDownLatch threadCompletedCounter = new CountDownLatch(THREAD_COUNT);

	/**
	 * 每隔 1小时,获得百度百科里的人物信息
	 */
	public void baiduBaikeRun() {
		if ("true".equalsIgnoreCase((String) config.getProperty("runBaiduBaikeJob", "false")) && mongoLockService.getLock(MongoLockService.LOCK_BAIDU_BAIKE)) {
			List<WeiboPerson> unBaikeFills = null;
			try {
				unBaikeFills = weiboPersonService.findUnBaikeFills(2000);
				if (unBaikeFills != null && !unBaikeFills.isEmpty()) {
					List<WeiboPerson> weiboPersonList = new ArrayList<WeiboPerson>();
					for (final WeiboPerson person : unBaikeFills) {
						// 获取百度百科信息
						WeiboPerson weiboPerson = BaiduBaikeUtils.getBaiduBaikeInfo(person.getName());
						weiboPerson.setId(person.getId());
						weiboPerson.setName(person.getName());
						weiboPerson.setTenantId(person.getTenantId());
						weiboPerson.setGroupId(person.getGroupId());
						
						// 更新状态
						if (!StringUtils.isEmptyString(weiboPerson.getDescription())) {
							weiboPerson.setFlagBaike(WeiboPerson.BAIKE_SUCCESS);
						} else {
							weiboPerson.setFlagBaike(WeiboPerson.BAIKE_FAIL);
						}
						weiboPerson.setLastUpdDate(new Date());
						weiboPersonList.add(weiboPerson);
					}
					// 首次更新或过期更新
					baiduBaikeRunInMultiThreads(weiboPersonList);
				}
			} catch (Exception e) {
				logger.error("update baidu baike error," + e.getClass().getName() + ": " + e.getMessage());
			} finally {
				try {
					mongoLockService.releaseLock(MongoLockService.LOCK_BAIDU_BAIKE);
				} catch (Exception e) {
					logger.error("release lock error ", e);
				}
			}
		}
	}
	
	/**
	 * 多线程处理:更新table
	 */
	private void baiduBaikeRunInMultiThreads(final List<WeiboPerson> weiboPersonList) {
		
		ExecutorService executor = Executors.newFixedThreadPool(THREAD_COUNT);
		for (int i = 0; i < THREAD_COUNT; i++) {
			executor.submit(new Runnable() {
				public void run() {
					WeiboPerson weiboPerson = getNext(weiboPersonList);
					while (null != weiboPerson) {
						try {
							// 首次更新或过期更新
							weiboPersonService.update(weiboPerson);
						} catch (Exception e) {
							logger.error("table weiboPerson update error ", e);
						}
						updCounter.incrementAndGet();
//						System.out.println("Thread:" + Thread.currentThread().getName() + ", counter:" + updCounter + ", name:" + weiboPerson.getName());
						weiboPerson = getNext(weiboPersonList);
					}
					threadCompletedCounter.countDown();
				}
			});
		}
		closeThreadPool(executor);
	}
	
	/**
	 * 同步处理:获取需要更新的一条微博人物
	 */
	private synchronized WeiboPerson getNext(List<WeiboPerson> weiboPersonList){
        if(next.intValue()>=weiboPersonList.size()) return null;
        next.incrementAndGet();
        return weiboPersonList.get(next.intValue()-1);
    }
	
	/**
	 * 关闭线程池
	 */
	private void closeThreadPool(final ExecutorService executor) {
		try {
			threadCompletedCounter.await();
			executor.shutdown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		// mock
		List<WeiboPerson> weiboPersonList = new ArrayList<WeiboPerson>();
		int i = 1;
		while (i <= 10) {
			WeiboPerson weiboPerson = new WeiboPerson();
			weiboPerson.setName("a" + i);
			weiboPersonList.add(weiboPerson);
			i++;
		}
		// test multi-thread
		BaiduBaikeJob baiduBaikeJob = new BaiduBaikeJob();
		baiduBaikeJob.baiduBaikeRunInMultiThreads(weiboPersonList);
	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值