Java Multithreaded Processing Task

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.