码迷,mamicode.com
首页 > 编程语言 > 详细

java多线程,httpclient多线程

时间:2014-12-04 14:09:15      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:http   io   ar   使用   sp   java   for   on   2014   

1、GetThread.java

package 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<Integer, Boolean> 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()== HttpStatus.SC_OK){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

}
2、ThreadPoolHttpClient.java

package 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 {
    // 线程池
    private ExecutorService exe = null;
    // 线程池的容量
    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 && uSize<=12){
            POOL_SIZE = 4;
        }
        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();
        /* 从连接池中取连接的超时时间 */
        ConnManagerParams.setTimeout(params, 1000);
        /* 连接超时 */
        HttpConnectionParams.setConnectionTimeout(params, 2000);
        /* 请求超时 */
        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(10);
        final HttpClient httpClient = new DefaultHttpClient(cm,params);

        // URIs to perform GETs on
        //final String[] urisToGet = urls;
        final List<String> finalUriGet=urlList;

        /*使用线程池*/
        for (int i = 0; i < finalUriGet.size(); i++) {
            final int j=i;
            //System.out.println("j的值:"+j);
            HttpHead httpHead = new HttpHead(finalUriGet.get(j));
            GetThread getThread = new GetThread(httpClient,httpHead,threadSignal,hashMap);
            getThread.setName("线程" + j);
            exe.submit(getThread);
        }
        threadSignal.await();
        exe.shutdown();
        System.out.println("Done");
        System.out.println("毫秒:"+(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多线程,httpclient多线程

标签:http   io   ar   使用   sp   java   for   on   2014   

原文地址:http://my.oschina.net/wfire/blog/352205

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!