ExecutorService线程池的使用

直接来实例:

 

    	ExecutorService executor = Executors.newFixedThreadPool(Constants.POOL_SIZE); //Constants.POOL_SIZE为开启多少线程比如20
    	
    	for(FcInfo fcInfo : fcInfos){
    		RemoteFileGetter fileGetter = new SPZRemoteFileGetter();
    		fileGetter.setIp(fcInfo.getIp());
    		fileGetter.setPort(fcInfo.getPort());
    		fileGetter.setPath(fcInfo.getRemotePath());
    		fileGetter.setDev_id(fcInfo.getDev_id());
    		fileGetter.setDev_name(fcInfo.getDev_name());
    		fileGetter.setDate(fcInfo.getDate());
    		executor.execute(fileGetter);//把fileGetter 加入线程池,记得fileGetter 要有run();方法
    	}
    	executor.shutdown();//关闭线程池

 

 

注:类位置:import java.util.concurrent.ExecutorService

你可能感兴趣的:(java线程池,executorService)