• TestThreadPoolExecutor.java


    package           ;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.List;
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.CookieStore;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.cookie.Cookie;
    import org.apache.http.impl.client.AbstractHttpClient;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    import org.apache.http.protocol.BasicHttpContext;
    import org.apache.http.protocol.HttpContext;

    public class TestThreadPoolExecutor {
        private static ThreadPoolExecutor threadpool;

        /**
         * 当一个任务通过execute(Runnable)方法欲添加到线程池时:
         * 1.如果此时线程池中的数量小于corePoolSize,即使线程池中的线程都处于空闲状态,也要创建新的线程来处理被添加的任务。
         * 2.如果此时线程池中的数量等于corePoolSize,但是缓冲队列workQueue未满,那么任务被放入缓冲队列。
         * 3.如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量小于maximumPoolSize,建新的线程来处理被添加的任务。
         * 4.如果此时线程池中的数量大于corePoolSize,缓冲队列workQueue满,并且线程池中的数量等于maximumPoolSize,那么通过
         * handler所指定的策略来处理此任务。也就是:处理任务的优先级为:核心线程corePoolSize、任务队列workQueue、最大线程maximumPoolSize
         * ,如果三者都满了,使用handler处理被拒绝的任务。
         * 5.当线程池中的线程数量大于corePoolSize时,如果某线程空闲时间超过keepAliveTime,线程将被终止。这样,线程池可以动态的调整池中的线程数。
         */
        /**
         * corePoolSize-线程池维护线程的最少数量/线程保有量,线程池总永久保存执行线程的数量。
         * maximumPoolSize-池中允许的最大线程数(采用LinkedBlockingQueue时没有作用),线程最多不能超过此属性设置的数量,当大于线程保有量后,会新启动线程来满足线程执行。
         * keepAliveTime-线程池维护线程所允许的空闲时间, 当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间,线程池维护线程所允许的空闲时间。
         * unit-keepAliveTime线程池维护线程所允许的空闲时间的单位:秒。
         * workQueue-执行前用于保持任务的队列(缓冲队列)。此队列仅保持由execute 方法提交的 Runnable 任务。
         * RejectedExecutionHandler-线程池对拒绝任务的处理策略(重试添加当前的任务,自动重复调用execute()方法)
         */
        
        public TestThreadPoolExecutor() {
            threadpool = new ThreadPoolExecutor(20, 200000, 20, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(2000000),
                    new ThreadPoolExecutor.DiscardOldestPolicy());
        }

        static String[] urisToGet = {    
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.baidu.com/",
                "http://www.dianping.com/"
            };

            public void submit(final HttpClient httpClient, final HttpGet httpget, final int id, final String CityUrl) {
                final HttpContext context = new BasicHttpContext();
                threadpool.execute(new Runnable() {
                    public void run() {
                        try {
                            HttpResponse response = httpClient.execute(httpget, context);
                            HttpEntity entity = response.getEntity();
                            if (entity != null) {
                                InputStream instream = entity.getContent();
                                try {
                                System.out.println("CityUrl:" + CityUrl+", flag:" + id + ", threadpool.getPoolSize():" + threadpool.getPoolSize() + ", thread name:" + Thread.currentThread().getName());
                                //Thread.sleep(10);
                                // CityGetPageTotal(CityUrl, IOUtils.toString(instream));
                                // GetCityIDList(id, IOUtils.toString(instream));
                                }
                                // catch (InterruptedException e) {
                                // e.printStackTrace();
                                // }
                                finally {
                                    instream.close();
                                }
                            }
                        } catch (Exception e) {
                            httpget.abort();
                            System.out.println("Error: " + id + ", url:" + CityUrl + ". " + e);
                        }
                    }
                });
            }
        public static void main(String[] args) throws ClientProtocolException, IOException {
            ThreadSafeClientConnManager tscm = new ThreadSafeClientConnManager();
            tscm.setMaxTotal(100);
            tscm.setDefaultMaxPerRoute(1);
            HttpClient httpclient = new DefaultHttpClient(tscm);
            CookieStore cookieStore = ((AbstractHttpClient) httpclient).getCookieStore();  
            
            DefaultHttpClient httpclient2 = new DefaultHttpClient();
            HttpGet httpget2 = new HttpGet("http://www.dianping.com/shangqiu");   
            HttpContext localContext2 = new BasicHttpContext();
            HttpResponse response2 = httpclient2.execute(httpget2, localContext2);
            HttpEntity entity2 = response2.getEntity();
            List<Cookie> cookies;
            cookies = ((AbstractHttpClient) httpclient2).getCookieStore().getCookies();  
            
            try {
                TestThreadPoolExecutor t = new TestThreadPoolExecutor();
                if (cookies.isEmpty()) {  
                    System.out.println("None");  
                } else {  
                    for (int j = 0; j < cookies.size(); j++) {  
                        cookieStore.addCookie(cookies.get(j));  
                        //System.out.println("- " + cookies.get(j).toString());  
                    }
                }
                for (int i = 0; i < urisToGet.length; i++) {
                    //System.out.println("i:" + i);
                    HttpGet httpget = new HttpGet(urisToGet[i]);
                    httpget.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/41.0");
                    httpget.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    httpget.addHeader("Accept-Language", "en-US,zh-CN;q=0.5");
                    t.submit(httpclient, httpget,i,urisToGet[i]);
                }
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                //Thread.sleep(1000 * 10);
                threadpool.shutdown();
                if (threadpool.isTerminated()) {
                    threadpool.shutdownNow();
                } else {
                    Thread.sleep(10);
                }
                System.out.println("................................................");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

  • 相关阅读:
    Linux sed命令
    Linux之read命令使用
    Linux shell之数组
    Linux Shell脚本攻略:shell中各种括号()、(())、[]、[[]]、{}的作用
    shell中的${},##, %% , :- ,:+, ? 的使用
    hostname命令
    进度条的制作-python
    33 Python 详解命令解析
    Python 学习笔记 多进程 multiprocessing--转载
    Spring注解(生命周期)
  • 原文地址:https://www.cnblogs.com/timssd/p/4898203.html
Copyright © 2020-2023  润新知