/*** Created by Lzd on June 6, 2016 PM 5:06:56*/ Public classjdpriceutils{Private Static FinalLogger log = Logger.getlogger (jdpriceutils.class); Private intThreads = 1; PrivateThreadPool ThreadPool; PrivateDbstore Store; PrivateIredis Redis =Redisfactory.getshardedredis (); PrivateReentrantlock LOCK =NewReentrantlock (); PrivateCondition Condition = This. Lock.newcondition (); Private Finallinkedlist<jdbookinfo> list =NewLinkedlist<>(); Private FinalObject List_lock =NewObject ();//private static final closeablehttpclient client = Httpclients.createdefault (); Public Interfacedbstore{List<JdBookInfo>get (); voidSave (Jdbookinfo book); } Publicjdpriceutils (Dbstore dbstore) { This. store =Dbstore; } Public voidinit () {ThreadPool=NewThreadPool ( This. Getthreads ()); while(true) { synchronized(List_lock) {List.addall (product ()); Log.info ("List container init size =" +list.size ()); } if(list.size () = = 0){ This. await (); }Else{consume (); This. Signal (); } } } protectedList<jdbookinfo>product () {returnStore.get (); } protected voidconsume () { while(List.size () > 0) { FinalJdbookinfo Jdbookinfo; synchronized(list_lock) {Jdbookinfo=List.removefirst (); } threadpool.execute (NewRunnable () {@Override Public voidrun () {Try{handleprice (jdbookinfo); } Catch(Exception e) {log.error ("Handle Price is Error", E); } } }); } } protected voidHandleprice (Jdbookinfo jdbookinfo) {log.info (Thread.CurrentThread (). GetName ()+ "--processing--" + jdbookinfo.getjsin () + "--Price"); if(Jdbookinfo = =NULL|| Jdbookinfo.getjsin () = =NULL) { NewIllegalArgumentException ("param is not valid"); } String Code=Jdbookinfo.getjsin (); String Res=NULL; while(Stringutils.isempty (res)) {res=GetPrice (code); } map Map=Collections.empty_map;; Try{String JSON= Res.substring (Res.indexof ("{"), Res.lastindexof ("}") +1); Map= Json.parseobject (Json,map.class); Log.info ("Request return JSON = [" + JSON + "]"); } Catch(Exception e) {addlist (jdbookinfo); Log.warn ("JSON parse is error, again back list"); return; } String Error= String.valueof (Map.get ("Error")); if(!" Null. Equals (Error)) {addlist (jdbookinfo); Log.warn ("Get pdos_captcha,list Add book = [Jsin =" +jdbookinfo.getjsin () + ";p rice =" +jdbookinfo.getpreface () + "or" +Jdbookinfo.getsaleprice ()); return; } String Price= String.valueof (Map.get ("M")); String Saleprice= String.valueof (Map.get ("P")); //if the price is NULL, set the default value to 0; if("null". Equals (Price)) { Price= "0"; } if("null". Equals (Saleprice)) {Saleprice= "0"; } jdbookinfo Book=NewJdbookinfo (); Book.setjsin (Jdbookinfo.getjsin ()); Book.setprice (price); Book.setsaleprice (Saleprice); Book.setid (Jdbookinfo.getid ()); Store.save (book); } Private voidaddlist (Jdbookinfo jdbookinfo) {synchronized(List_lock) {list.addlast (jdbookinfo); } } protectedstring GetPrice (String code) {string URL= "Http://p.3.cn/prices/get?skuid=J_" +code+ "&TYPE=1&AREA=1_72_2840&CALLBACK=CNP"; String Res=NULL; Try{String Srandmember=NULL; String[] Split=NULL; while(Srandmember = =NULL) {Srandmember= Redis.srandmember ("proxies"); if(Srandmember = =NULL)Continue; Split= Srandmember.split (":"); } httphost httphost=NewHttphost (Split[0],integer.parseint (split[1])); Requestconfig Reqconfig=Requestconfig.custom (). SetProxy (Httphost). Setconnectionrequesttimeout (10000). Setconnecttimeout (10000). SetSocketTimeout (10000). build (); Log.info ("Request Httphost =" + split[0]+ ":" +split[1]); Requestbuilder Requestbuilder=requestbuilder.get (URL). Setconfig (Reqconfig); Closeablehttpresponse Response=Httpclients.createdefault (). Execute (Requestbuilder.build ()); InputStream in=response.getentity (). getcontent (); byte[] ByteArray =Ioutils.tobytearray (in); Res=NewString (ByteArray, "Utf-8"); } Catch(Exception e) {log.error ("Get conntion price URL is error"); } returnRes; } Private voidawait () { This. Lock.lock (); Try{log.info ("Main Thrad is await ..."); This. condition.await (30000L, Timeunit.milliseconds); } Catch(Exception e) {}finally { This. Lock.unlock (); } } Private voidsignal () { This. Lock.lock (); Try { This. condition.signal (); } Catch(Exception e) {}finally { This. Lock.unlock (); } } Public intgetthreads () {returnthreads; } PublicJdpriceutils Setthreads (intthreads) { This. threads =threads; return This; } Public voidstart () { This. Init (); } Public classthreadpool{Private intthreads; PrivateExecutorservice Executorservice; PrivateAtomicinteger threadactive =NewAtomicinteger (0); PrivateReentrantlock Poollock =NewReentrantlock (); PrivateCondition poolcondition = This. Poollock.newcondition (); PublicThreadPool (intthreads) { This. threads =threads; Executorservice=Executors.newfixedthreadpool (threads); } Public voidExecuteFinalRunnable Runnable) { Try{poollock.lock (); if(Threadactive.get () >=threads) { while(Threadactive.get () >=threads) { Try{poolcondition.await (); } Catch(Exception e) {e.printstacktrace (); } } } } Catch(Exception e) {}finally{poollock.unlock (); } threadactive.incrementandget (); Executorservice.execute (NewRunnable () {@Override Public voidrun () {runnable.run (); Try{poollock.lock (); Poolcondition.signal (); Threadactive.decrementandget (); } Catch(Exception e) {}finally{poollock.unlock (); } } }); } }}
Encapsulate your own threadpool.