zoukankan      html  css  js  c++  java
  • Executor多线程框架

    啥都别说了,上代码:

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ThreadPoolExecutor;
    
    public class ExecutorTest {
    	
    	private static Integer pages=1;
    	private static boolean exeFlag=true;
    
    	public static void main(String[] args) {
    		ExecutorService executorService=Executors.newFixedThreadPool(10);//创建ExxcutorService线程池
    		while(exeFlag){
    			if(pages<=100){
    				executorService.execute(new Runnable(){
    
    					@Override
    					public void run() {
    						// TODO Auto-generated method stub
    						System.out.println("爬取了第"+pages+"网页....");
    						pages++;
    					}
    					
    				});
    			}else{
    				if(((ThreadPoolExecutor)executorService).getActiveCount()==0){
    					//活动线程个数为o
    					executorService.shutdown();
    					exeFlag=false;
    					System.out.println("爬虫任务已经完成");
    				}
    			}
    			try {
    				Thread.sleep(100);
    			} catch (InterruptedException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		
    	}
    }



  • 相关阅读:
    奶酪真香
    规格说明书
    33
    111
    出题
    随笔 01
    我爱奶酪
    用户规格说明书
    第二次结对作业
    结对作业1
  • 原文地址:https://www.cnblogs.com/ryelqy/p/10104154.html
Copyright © 2011-2022 走看看