java多线程处理任务

最近用到使用多线程处理给用户发送站内消息的问题,想到使用java自带的线程池进行处理这个问题,具体如下:
定义一个线程:

package com.qlwb.util;

import org.apache.log4j.Logger;

import redis.clients.jedis.ShardedJedis;

import com.qlwb.common.redispool.BusinessRedisParam;
import com.qlwb.common.redispool.BusinessRedisPool;
/**
 * 
 * 
 * @类编号:
 * @类名称:DealUserTools
 * @内容摘要: 消息处理
 * @author:鹿伟伟
 * @创建日期:2016年6月18日 下午1:23:03
 * @修改人:
 * @修改日期:
 * @修改描述:简单描述修改的内容
 * @version 1.0.0
 *
 */
public class DealUserTools extends Thread{
    private final Logger logger = Logger.getLogger(DealUserTools.class);
    private int minId=0;
    private int maxId=0;
    public DealUserTools(int minId,int maxId){
        this.maxId=maxId;
        this.minId=minId;
    }
    public void run(){
        ShardedJedis jedis=null;
        logger.info("站内消息处理开始");
        long t11=System.currentTimeMillis();

        try {
            //jedis=BusinessRedisPool.getInstance().getJedis();
            for(int i=minId;i<=maxId;i++){
                logger.info(Thread.currentThread().getName()+"=="+i);
                //TODO
                //业务处理
                }
            }
        } catch (Exception e) {
            logger.error("站内消息状态设置异常:", e);
        }finally{
            BusinessRedisPool.getInstance().returnResource(jedis);
        }
        long t12=System.currentTimeMillis();
        System.out.println("站内消息处理用时"+(t12-t11)+"ms");
    }
    public static void main(String[] args) {

    }
}

使用线程池

ExecutorService pool = Executors.newFixedThreadPool(count);
        Map<String,Object> mapParam=new HashMap<String, Object>();
        try {

            for(int i=0;i<=count;i++){
                Thread t1= new DealUserTools(minId,maxId);
                pool.execute(t1);
            }
            pool.shutdown();
        } catch (Exception e) {
            logger.error("站内消息getUserId:", e);
        }
posted @ 2016-06-18 14:27  奋斗鹿  阅读(276)  评论(0编辑  收藏  举报