Recently used in the use of multithreading to send the message to the user in the station, the thought of using Java's own thread pool to handle the problem, as follows:
Define a thread:
PackageCom.qlwb.util;ImportOrg.apache.log4j.Logger;ImportRedis.clients.jedis.ShardedJedis;ImportCom.qlwb.common.redispool.BusinessRedisParam;ImportCom.qlwb.common.redispool.BusinessRedisPool;/** * * @ class Number: * @ class Name: Dealusertools * @ Content Summary: Message processing * @author: Lu Weiwei * @ created: June 18, 2016 1:23:03 * @ Modified By: * @ Modify Date: * @ Modify Description: Simple description of Modified content * @version 1.0.0 * */ Public class dealusertools extends Thread{ Private FinalLogger Logger = Logger.getlogger (Dealusertools.class);Private intMinid=0;Private intMaxid=0; Public Dealusertools(intMiniD,intMAXID) { This. Maxid=maxid; This. Minid=minid; } Public void Run() {Shardedjedis jedis=NULL; Logger.info ("in-Station message processing started");LongT11=system.currenttimemillis ();Try{//jedis=businessredispool.getinstance (). Getjedis (); for(inti=minid;i<=maxid;i++) {Logger.info (Thread.CurrentThread (). GetName () +"=="+i);//todo //Business processing} } }Catch(Exception e) {Logger.error ("Message status setting exception in station:", e); }finally{businessredispool.getinstance (). Returnresource (Jedis); }LongT12=system.currenttimemillis (); System.out.println ("in-Station message processing time"+ (T12-T11) +"MS"); } Public Static void Main(string[] args) { }}
Using the thread pool
ExecutorService pool = Executors.newFixedThreadPool(count); Map<String,Object> mapParam=new HashMap<StringObject>(); try { for(int i=0;i<=count;i++){ new DealUserTools(minId,maxId); pool.execute(t1); } pool.shutdown(); catch (Exception e) { logger.error("站内消息getUserId:", e); }
Java Multithreaded Processing Task