JAVA使用timetask管理线程池时出现错误

发布于 2016/09/29 12:29
阅读 207
收藏 0

要定时爬取代理网站上的代理Ip 开了一个线程池用来爬取 用timetask来进行定时

public List<String> startWork(){
		List<String> proxyIP = Collections.synchronizedList(new ArrayList<String>());
		proxyIP = getProxy(getDoc(URL));
		System.out.println("待检验ip数量:"+proxyIP.size());
		List<String> provedIP = proveProxy(proxyIP);
		return provedIP;
	}
	
	public static List<String> proveProxy(List<String> proxyIP){
		
		Iterator<String> it1 = proxyIP.iterator();
		while(it1.hasNext()){
			final String tmp = it1.next();
			//proxyTest.setProxy(ip, port);
			
			fixTreadPool.execute(new Runnable(){
				@Override
				public void run() {
					String ip = tmp.split(":")[0];
					int port =  Integer.parseInt((tmp.split(":")[1]));

					if(ProxyTest.proxyTest(ip, port)){
						provedIP.add(tmp);
					}
				}				
			});
		}
		fixTreadPool.shutdown();    
		while (!fixTreadPool.isTerminated()) {     //全部线程执行结束判定 
        }
        System.out.println("验证结束");
        System.out.println("检测到可用代理IP数量:"+provedIP.size());
        return provedIP;            
	}



public void startProxyCrawler(){   //开启代理爬虫 并添加新可用代理Ip到proxyQueue中
		TimerTask tt = new TimerTask(){
			public void run(){
				ProxyCrawler proxyCrawler = new ProxyCrawler();
				List<String> provedIP = proxyCrawler.startWork();
				addProxyQueue(provedIP);
			}
		};
		Timer timer = new Timer();
		//long intervel = 1*60*1000;       //爬取代理网站间歇时间
		long intervel = 500;
		long delay = 0;
		timer.scheduleAtFixedRate(tt, delay,intervel);
	}



异常信息如下 我是想要在20分钟后重新开一个线程池的 但是不知道为什么上一个线程池shutdown后会抛出下面异常

Exception in thread "Timer-0" java.util.concurrent.RejectedExecutionException: Task ProxyCrawler$1@7cb1a4f1 rejected from java.util.concurrent.ThreadPoolExecutor@474bcd39[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 100]

    at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
    at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
    at ProxyCrawler.proveProxy(ProxyCrawler.java:46)
    at ProxyCrawler.startWork(ProxyCrawler.java:35)
    at MyWork$2.run(MyWork.java:97)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)
加载中
0
业余编程人士
业余编程人士
能不能贴代码的方式是文字的方式 我也可以复制执行以下 
好的 我重新编辑 麻烦您帮忙看一下是什么问题
返回顶部
顶部