Java 在守护进程中使用Executer

Java 在守护进程中使用Executer,java,threadpool,executorservice,Java,Threadpool,Executorservice,我们有一个守护进程,它必须每次执行并寻找要处理的新请求。由于我们有5台服务器需要执行该进程,当前守护进程正在使用executer服务的newFixedThreadPool创建5个线程。一旦工作完成,我们将关闭执行器 在生产unix服务器中部署此作业时,在运行几天后,它被挂起。通过日志,我们看到所有executer线程都已完成其作业,但在执行shutdown后,它被挂起。我假设在这种情况下,executer并没有正确关闭,因为我们正在检查它是否在for循环中终止。该循环可能会不确定地运行,因此没有

我们有一个守护进程,它必须每次执行并寻找要处理的新请求。由于我们有5台服务器需要执行该进程,当前守护进程正在使用executer服务的newFixedThreadPool创建5个线程。一旦工作完成,我们将关闭执行器

在生产unix服务器中部署此作业时,在运行几天后,它被挂起。通过日志,我们看到所有executer线程都已完成其作业,但在执行shutdown后,它被挂起。我假设在这种情况下,executer并没有正确关闭,因为我们正在检查它是否在for循环中终止。该循环可能会不确定地运行,因此没有进一步的处理。请分享你的想法还有其他原因

代码:


当(!executor.isTerminated())时,切勿执行此操作。这就是为什么。忙碌的等待(几乎)总是错误的。您在这里等待的时间也很忙(batchtype==2)。请向我们展示您的
ExecuteThread
-如何关闭它。除非其线程处于空闲状态,
ExecutorService
无法关闭
Shutdownow
仅当线程可中断时才起作用。在下一次迭代中创建新的线程池之前,使用isTerminated确保所有线程都已终止。现在,如果Shutdownow在15分钟内未完成,我们将等待Shutdownow在10分钟内完成。是否存在Shutdownow甚至无法终止的情况?。执行线程是一个可运行的类。我们如何确保线程是可中断的..使用,我理解您为什么使用
isTerminated
,但它仍然在忙着等待。它束缚了一个CPU核心,完全不做任何事情。一个完整的核心在100%没有理由。在服务器上,这是不合理的。至于你的
ExecuteThread
,你需要发布它的代码——如何让它保持活力以及如何让它停止。是的,我理解你的担忧……这就是为什么我在新代码中对它进行了评论。下面是executeThread代码。我假设只要调用executer.shutdown(),线程就会停止。在executeThread中,我只是调用一些其他业务类。
do {
    executor = Executors.newFixedThreadPool(5);
    startTime=System.currentTimeMillis();

    for (int i = 0; i < str.length(); i++) {
        if(str.charAt(i)!='-'||str.charAt(i)!=','||str.charAt(i)!='.'||str.charAt(i)!='|') {    
            if(str.charAt(i)=='5'||str.charAt(i)=='6'||str.charAt(i)=='7'||str.charAt(i)=='8'||str.charAt(i)=='9') {
                String s=str.substring(i, i+1);
                Runnable worker = new ExecuteThread(s,batchtype);
                executor.execute(worker);
            }
        }
    }

    executor.shutdown();
    while (!executor.isTerminated()) { }
} while(batchtype == 2);
do {
    executor = Executors.newFixedThreadPool(5);
    startTime=System.currentTimeMillis();

    for (int i = 0; i < str.length(); i++) {
        if(str.charAt(i)!='-'||str.charAt(i)!=','||str.charAt(i)!='.'||str.charAt(i)!='|') {    
            if(str.charAt(i)=='5'||str.charAt(i)=='6'||str.charAt(i)=='7'||str.charAt(i)=='8'||str.charAt(i)=='9') {
                String s=str.substring(i, i+1);
                Runnable worker = new ExecuteThread(s,batchtype);
                executor.execute(worker);
            }
        }
    }
    executor.shutdown();

    // FIX
    try {
        if (!executor.awaitTermination(900,TimeUnit.SECONDS)) { 
            logger.info("Executor did not terminate in the specified time.");
            List<Runnable> droppedTasks = executor.shutdownNow();
            if (!executor.awaitTermination(600,TimeUnit.SECONDS)){
                logger.info("Even shutdownNow did not terminated in the specified time."); 
            }
            logger.info("Executor abruptly shut down. " + droppedTasks.size() + " tasks will not be executed.");
        }
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        logger.error(e.getMessage());
    }

    //while (!executor.isTerminated()) {
    //}

    stopTime=System.currentTimeMillis();
    logger.info("Time taken by executer to finish job with batchType "+batchtype+ " in sec " +(stopTime-startTime)/1000);
} while(batchtype==2);// to run batch-2 in daemon mode
public class ExecuteThread extends AbstractMaxisBatchSystem implements Runnable {

    private String serverId;
    private int batchtype;

    public ExecuteThread(String s,int batchtype) throws MaxisBatchFatalException {
                this.serverId=s;
                this.batchtype=batchtype;
        }


    private static Logger logger = null;
    static{
        try{
            logger = logger.getLogger(com.maxis.rnb.batch.realReinstatement.FetchNewPayments.ExecuteThread.class);
        }
        catch(Exception ex) { }
    }       

    /*public ExecuteThread(String s, int batchtype2) throws MaxisBatchFatalException{
        // TODO Auto-generated constructor stub
    }*/


    public void run() 
    {
        try
        {
            if(batchtype==1)
            {   
                logger.info("FetchNewPayment Batch started for serverID: " +serverId);
                FetchNewPayment batch = new FetchNewPayment();
                batch.execute(serverId);
            }
            else if(batchtype==2)
            {
                //do{
                logger.info("ProcessNewPayment Batch started for serverID: " +serverId);
                ProcessNewPayment batch = new ProcessNewPayment();
                batch.execute(serverId);
                //}
                //while(true);
            }
            else if(batchtype==3)
            {
                logger.info("HouseKeeping Batch started for serverID: " +serverId);
                RRHouseKeeping batch = new RRHouseKeeping();
                batch.execute(serverId);
            }

        }
         catch (MaxisBatchFatalException e)
            {
             logger.error("ServerId: "+serverId+":"+e.getMessage(), e);
            // System.exit(EXIT_FATAL);
             //throw new MaxisBatchFatalException(e.getMessage(), e);
            }

    }

}