zoukankan      html  css  js  c++  java
  • 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);
            }
  • 相关阅读:
    [AU3]技巧
    [AU3]Windows 10 Update message DETELEDER Win10更新通知删除助手
    vue多选框选择后显示选中的内容
    计算object的长度
    关于苹果手机点击事件无效的解决办法
    vue中将汉字按照首字母排序,也适用于其他地方,但不适用多音字
    关于json数据格式错误
    Java下使用Swing来进行图形界面开发
    数字与静态
    Java中的内存机制及管理
  • 原文地址:https://www.cnblogs.com/luweiwei/p/5968471.html
Copyright © 2011-2022 走看看