线程池关闭

线程池关闭:

package com.audaque.tjfxpt.transfer.main;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.audaque.tjfxpt.neo4j.enums.EnumNodeType;
import com.audaque.tjfxpt.neo4j.util.Neo4jUtil;
import com.audaque.tjfxpt.transfer.runnable.ApRunnable;
import com.audaque.tjfxpt.transfer.service.ApServiceImpl;
import com.audaque.tjfxpt.transfer.util.PageUtil;

/**
 * 从sdi_t_ap表取得经纬度数据,调用接口,得到经纬度对应的街道、社区,保存街道社区信息
 * @author sniper
 *
 */
public class ApMain {
    
    public static Logger logger = Logger.getLogger(ApMain.class);
    
    public static void main(String[] args) {
        long beginTime = System.currentTimeMillis();
        
        int pageSize = 1000;
        int processors = Runtime.getRuntime().availableProcessors();
//        int poolSize = processors*2;
        int poolSize = 5;
        int threadSize = 5;
        
        if(null != args && args.length > 0) {
            pageSize = Integer.parseInt(args[0]);
            
            if(args.length > 1) {
                poolSize = Integer.parseInt(args[1]);
            }
            
            if(args.length > 2) {
                threadSize = Integer.parseInt(args[2]);
            }
        }
         
        ExecutorService service = Executors.newFixedThreadPool(poolSize);
        
        //人口
        execute("DWR_MDM_PPU_RESIDENT_DIM_INFO", pageSize, EnumNodeType.PERSON, service, threadSize, "resident_Id".toUpperCase());
        
        //法人
        execute("dwr_mdm_legal_person_dim", pageSize, EnumNodeType.LEGALPERSON, service, threadSize, "legal_person_id".toUpperCase());
        
        //房屋
        execute("dwr_mdm_house_info_dim", pageSize, EnumNodeType.HOUSE, service, threadSize, "house_id".toUpperCase());
        
        //楼栋
        execute("dwr_mdm_build_info_dim", pageSize, EnumNodeType.BUILDING, service, threadSize, "building_info_id".toUpperCase());
        
        //事件
        execute("dwr_evt_house_event", pageSize, EnumNodeType.EVENT, service, threadSize, "manage_event_id".toUpperCase());
        
        /*
         * Initiates an orderly shutdown in which previously submitted
         * tasks are executed, but no new tasks will be accepted.
         */
        service.shutdown();
        
        /*
         * if all task still not finish, sleep 5 seconds and check again  
         */
        while(!service.isTerminated()) {
            try {
                service.awaitTermination(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        Neo4jUtil.shutdown();
        logger.info("execute time:" + (System.currentTimeMillis()-beginTime)/1000 + "(s)");
    }
    
    public static void execute(String tableName, int pageSize, EnumNodeType enumNodeType, ExecutorService service, int threadSize, String idKey) {
        int totalCount = ApServiceImpl.getInstance().queryTotalCount(tableName);
        int totalPage = PageUtil.getInstance(totalCount, 0, pageSize).getTotalPages();
        
        ApRunnable apR = new ApRunnable(totalCount, pageSize, totalPage, tableName, enumNodeType, idKey);
        for(int i=0; i<threadSize; i++) {
            service.execute(apR);
        }
    }
    
}


你可能感兴趣的:(线程池关闭)