Java Multi-threading, HttpClient multithreading

Source: Internet
Author: User

1, getthread.javapackage com.aa.controller.xx;import org.apache.http.httpresponse;import  org.apache.http.httpstatus;import org.apache.http.client.httpclient;import  org.apache.http.client.methods.httphead;import org.apache.http.protocol.basichttpcontext;import  org.apache.http.protocol.httpcontext;import java.util.arraylist;import java.util.list;import  java.util.concurrent.concurrenthashmap;import java.util.concurrent.countdownlatch;/** * created  by administrator on 2014/12/4. */public  class getthread extends  Thread{    private final HttpClient httpClient;     private final HttpContext context;    private final  httphead httphead;    private final countdownlatch threadsignal;     private final concurrenthashmap<Integer,Boolean> hashMap;    private static List<Thread>  Runningthreads = new arraylist<thread> ();     public getthread ( httpclient httpclient, httphead httphead, countdownlatch threadsignal,  CONCURRENTHASHMAP&LT;INTEGER,&NBSP;BOOLEAN&GT;&NBSP;HASHMAP)  {         this.httpclient = httpclient;        this.context  = new basichttpcontext ();         this.httphead =  httpHead;        this.threadSignal = threadSignal;         this.hashmap = hashmap;    }      @Override     public void run () {         //system.out.println ("ABC:" +thread.currentthread (). GetName ());        boolean  B=get ();         hashmap.put ((int)  threadsignal.getcount (),  b );         threadsignal.countdown ();    }     public boolean get ()  {         httpresponse response = null;        try {             response = httpclient.execute ( Httphead);             if (Response.getStatusLine (). Getstatuscode () ==&NBSP;HTTPSTATUS.SC_OK) {                 return true;             }else{                return false;             }         } catch  (exception e)  {             return false;        }    }}
2, Threadpoolhttpclient.javapackage com.aa.controller.xx;import com.aa.utilities.sysuserutil;import  org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpHead;import  org.apache.http.conn.params.connmanagerparams;import org.apache.http.conn.scheme.plainsocketfactory; Import org.apache.http.conn.scheme.scheme;import org.apache.http.conn.scheme.schemeregistry;import  org.apache.http.impl.client.DefaultHttpClient;import  org.apache.http.impl.conn.poolingclientconnectionmanager;import org.apache.http.params.basichttpparams; Import org.apache.http.params.httpconnectionparams;import org.apache.http.params.httpparams;import  java.util.Date;import java.util.List;import java.util.Map;import  java.util.concurrent.concurrenthashmap;import java.util.concurrent.countdownlatch;import  Java.util.concurrent.executorservice;import java.util.concurrent.executors;public class threadpoolhttpclient {    //  thread Pool     private executorservice  exe = null;    //  thread pool Capacity     private static   int POOL_SIZE = 5;    private HttpClient client  = null;    list<string> urllist = null;     Public threadpoolhttpclient (list<string> urllist) {         this.urlList=urlList;    }    public ConcurrentHashMap< Integer,boolean> test ()  throws Exception {         int usize = urllist.size ();         if (uSize>0 &NBSP;&AMP;&AMP;&NBSP;USIZE&LT;=12) {             pool_size = 4;&Nbsp;       }        if (uSize>30 ) {            pool_size = 6;         }        Date d1 =  New date ();         final countdownlatch threadsignal  = new countdownlatch (Urllist.size ());         final  concurrenthashmap<integer,boolean> hashmap = new concurrenthashmap ();         exe = executors.newfixedthreadpool (POOL_SIZE);         httpparams params =new basichttpparams ();         /*  timeout for fetching connections from the connection pool  */         Connmanagerparams.settiMeout (params, 1000);        /*  connection Timeout  */         httpconnectionparams.setconnectiontimeout (params, 2000);         /*  Request Timeout  */         Httpconnectionparams.setsotimeout (params, 4000);         Schemeregistry schemeregistry = new schemeregistry ();         schemeregistry.register (                 new scheme ("http",  80, plainsocketfactory.getsocketfactory ()));         //ClientConnectionManager cm = new  Poolingclientconnectionmanager (schemeregistry);         Poolingclientconnectionmanager cm=new poolingclientconNectionmanager (Schemeregistry);         cm.setmaxtotal ();         final HttpClient httpClient = new  Defaulthttpclient (cm,params);         // uris to perform  gets on        //final string[] uristoget =  urls;        final list<string> finaluriget= urllist;        /* using the thread pool */         for  (Int i = 0; i < finaluriget.size ();  i++)  {             final int j=i;             //system.out.println ("Value of J:" +j);           &nbsP; httphead httphead = new httphead (Finaluriget.get (j));             getthread getthread = new getthread ( HTTPCLIENT,HTTPHEAD,THREADSIGNAL,HASHMAP);             getthread.setname ("thread"  + j);             exe.submit (GetThread);        }         threadsignal.await ();         exe.shutdown ();         system.out.println ("Done");         system.out.println ("MS:" + (New date (). GetTime ()-d1.gettime ()));         return hashMap;    }    public  concurrenthashmap<integer,boolean>  Gethashmapbyurls () {        threadpoolhttpclient  Threadpoolhttpclient = new threadpoolhttpclient (urllist);         try {             Concurrenthashmap<integer,boolean> hashmap =threadpoolhttpclient.test ();             for (map.entry<integer,boolean> e: Hashmap.entryset ()) {                 system.out.println ("Key:" +e.getkey () + "_____| | |  value: "+e.getvalue ());            }             return  hashMap;         } catch  (exception e)  {             e.printstacktrace ();        }         return null;    }}


Java Multi-threading, HttpClient 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.