【问题标题】:ThreadpoolExecutor and main thread executing in parallelThreadpoolExecutor 和主线程并行执行
【发布时间】:2017-04-08 17:43:31
【问题描述】:

线程池执行器与主线程并行执行。主线程不会等到执行器关闭。

public static void main(String[] args) {
        Date jobStartTime = null;


        LOGGER.info("MainApp::Job started");
        try {

            MainApp obj = new MainApp();
            // Getting the job Id of the job
            String jobName=args[0]; //batch name
            String fileName=args[1]; //sqoop file

            LOGGER.info("MainApp::jobName: "+jobName+" fileName "+fileName);

            currentJobID = obj.getMaxJobId(jobName);

            LOGGER.info("MainApp:Job Id is" + currentJobID);

            // Getting the start time of the job
            jobStartTime = commonDB.getTime();
            LOGGER.info("MainApp:Job Start time is" + jobStartTime);

            JobDetails job=new JobDetails(currentJobID,jobName,fileName);

            // Reading and parsing the sqoop file and executing the sqoop commands
            CommandGenerator exec=new CommandGenerator();
            List<TableInfo> commandList = exec.parseAndExec(job);

            ThreadPoolExecutor tp = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
            for (final TableInfo table : commandList) {
                ParallelExecutor pe = new ParallelExecutor(table);
                tp.execute(pe);
            }

            tp.shutdown();

            while(!tp.isShutdown()){

            }

            job=new JobDetails(currentJobID,jobName,fileName,jobStartTime);
            //put everything in one method
            StatusAndMailUtils status=new StatusAndMailUtils();
            status.onJobCompletion(job);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            LOGGER.info("MainApp::Exception");
            e.printStackTrace();
        }

    }

我使用了 while 循环来保持主线程等待意味着执行线程正在进行时。但是,它没有帮助。请告诉我如何让主线程等待。

while(!tp.isShutdown()){

                }

【问题讨论】:

    标签: java multithreading threadpoolexecutor


    【解决方案1】:

    调用shutdown() 后,您可以使用awaitTermination(long timeout, TimeUnit unit) 阻塞调用线程,直到所有任务执行完毕。

    如果您想等待任务完成所需的时间,您可以使用一个过大的值作为超时,但是如果任务永远不会结束,它可能会让您的线程永远等待,所以最好设置一个合理的超时,以便在异常过长时执行某些任务。

    例如:

    tp.shutdown();
    tp.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
    

    【讨论】:

      【解决方案2】:

      当然不是等待。这就是创建线程池的全部想法,因此您的主线程可以在线程池执行其他任务时执行其他任务。

      您可以使用awaitTermination(long timeout, TimeUnit unit) 方法让您的主线程在线程池完成其任务时暂停。

      【讨论】:

      • 问题是,我正在运行的任务运行时间很长,我不确定这些任务何时完成。所以,我不能提供超时。
      • @angularBeginner 为什么不呢?您肯定会知道它们将在不到 10 年的时间内完成。
      【解决方案3】:

      您也可以提交这些 Runnable 并等待它们完成。也可以在抛出异常之前指定超时等待线程执行。

      List<Future<ParallelExecutor>> tasks = new ArrayList<>();
      ExecutorService tp = Executors.newFixedThreadPool(10);
      for (final TableInfo table : commandList) {
         ParallelExecutor pe = new ParallelExecutor(table);
         tasks.add(tp.submit(pe));
      }
      
      for (Future<ParallelExecutor > p : tasks) {
         p.get(); // with timeout p.get(10, TimeUnit.SECONDS);
      }
      
      tp.shutdown();
      

      【讨论】:

        猜你喜欢
        • 2023-01-27
        • 1970-01-01
        • 2014-07-18
        • 2017-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-05
        相关资源
        最近更新 更多