160801, Blockingqueue processing multithreading

Source: Internet
Author: User

Described in the previous spring Taskexecutor, today introduces a JDK in the process of multi-threading method One, spring configuration file (inject bean) <bean id= "cmsclickbuttonmng" class= " Com.xxx.manager.main.impl.CmsClickButtonMngImpl "/> Two, thread class Customerbutton.javaimport Java.util.concurrent.blockingqueue;import Nl.bitwalker.useragentutils.useragent;import Org.apache.commons.lang.stringutils;import Org.springframework.web.context.contextloader;import Org.springframework.web.context.webapplicationcontext;import Com.xxxx.cms.entity.main.cmsclickbutton;import Com.xxxx.cms.manager.main.cmsclickbuttonmng;import com.xxxx.common.util.UserAgentUtils; @SuppressWarnings ("    Rawtypes ") public class Consumerbutton implements runnable{public static Boolean running = FALSE;    protected Webapplicationcontext CTX;      Private CMSCLICKBUTTONMNG cmsclickbuttonmng;//The class to be processed protected blockingqueue queue = null;    protected static int i = 0;      Public Consumerbutton (Blockingqueue queue) {this.queue = queue; public void Run () {try {System.Out.println ("Queue size:" + queue.size ());                  while (!queue.isempty ()) {Cmsclickbutton cb = (Cmsclickbutton) queue.take ();                if (CB! = null) {record (CB);        }} consumerbutton.running = false;          } catch (Interruptedexception e) {e.printstacktrace (); }}/** Business */public void record (Cmsclickbutton cb) {try {if (CB! = null) {if (S                    Tringutils.isnotblank (Cb.getagent ())) {String agent = cb.getagent (); String Customermodel = Useragentutils.getcustomermodel (agent);//Phone Model Cb.setcustomermodel (Customermodel)                    ;                    UserAgent useragent = useragent.parseuseragentstring (agent); if (useragent! = null) {String browsername = string.valueof (Useragent.getbrowser (). GetName ());//Browser class Type String OperatingSystem = String.valueOf (Useragent.getoperatingsystem (). GetName ());//operating system type String browserversion =string.valueof (userage Nt.getbrowserversion ());//Browser version Boolean IsMobileDevice = Useragent.getoperatingsystem (). Ismobiledev                        Ice ();//Whether it is a mobile device cb.setbrowsername (browsername);                        Cb.setbrowserversion (browserversion);                        Cb.setismobiledevice (IsMobileDevice);                    Cb.setoperatingsystem (OperatingSystem);                }} Webapplicationcontext WAC = Contextloader.getcurrentwebapplicationcontext ();                CMSCLICKBUTTONMNG = (cmsclickbuttonmng) wac.getbean ("cmsclickbuttonmng");                CMSCLICKBUTTONMNG.SAVECB (CB);                consumerbutton.i++;            System.out.println ("Finish ..." +consumerbutton.i);        }} catch (Exception e) {e.printstacktrace (); }}} III, Producer.javaimport java.util.concurrent.BlOckingqueue;import Com.gmiao.cms.entity.main.cmsclickbutton;import com.gmiao.cms.entity.main.cmstrafficpage;@      Suppresswarnings ("Rawtypes") public class Producer implements Runnable {protected Blockingqueue queue = null; protected cmstrafficpage TP = NULL; Product one protected Cmsclickbutton cb = NULL;          Product two public Producer (blockingqueue queue,cmstrafficpage tp) {this.queue = queue;    THIS.TP = TP;          } public Producer (Blockingqueue Queue,cmsclickbutton cb) {this.queue = queue;    THIS.CB = CB; } @SuppressWarnings ("Unchecked") public void Run () {try {if (TP! = null) {Q            Ueue.put (TP);            }else if (cb! = null) {queue.put (CB);          }} catch (Interruptedexception e) {e.printstacktrace (); }}} IV, SPRINGMVC/** * parameter refers to the maximum capacity of the queue */public static Blockingqueue queue = new Arrayblockingqueue (10000)     ; @RequestMapping (value= "/save. JSPF ") public void Save (String pid,string openid,string pagename,httpservletrequest request,httpservletresponse respo NSE) {try {//If the project ID or page name is empty, then no record if (Stringutils.isblank (PID) | | Stringutils.isblank (PageName)) {Ajaxerrortojson (response, NULL, "Project ID or page name cannot be empty!")                ");            return;            } cmstrafficpage TP = new Cmstrafficpage ();            Tp.setpid (PID);            Tp.setdate (New Date ());            Tp.setstaytime (0l);            Tp.setip (REQUESTUTILS.GETIPADDR (request));//User IP address Tp.setpagename (pageName); Tp.setpageurl (Request.getheader ("Referer"));//the page link that initiated the Request Tp.setsessionid (Request.getsession (). GetId ());//user Sessi                Onid String Agent = Request.getheader ("user-agent");//Client Information if (Stringutils.isnotblank (agent)) {            Tp.setagent (agent); } blockingqueue queue = action or controller Producer Producer where trafficpageact.queue;//is located= new Producer (QUEUE,TP);            New Thread (producer). Start (); if (!                  consumer.running) {Consumer Consumer = new Consumer (queue);                New Thread (consumer). Start ();            Consumer.running = true; }} catch (Exception e) {log.error ("Record page access Error!            ", e); Ajaxerrortojson (response, NULL, "Record page access Error!            ");        return; }} I'm just using Blockqueue in the project code, to learn or learn about articles that can be viewed in one of the following pages http://wsmajunfeng.iteye.com/blog/1629354 or view the JDK documentation

  

160801, Blockingqueue processing multithreading

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.