Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ThreadpoolExecutor和主线程并行执行_Java_Multithreading_Threadpoolexecutor - Fatal编程技术网

Java ThreadpoolExecutor和主线程并行执行

Java ThreadpoolExecutor和主线程并行执行,java,multithreading,threadpoolexecutor,Java,Multithreading,Threadpoolexecutor,线程池执行器与主线程并行执行。主线程不会等待执行器关闭 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

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

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();
        }

    }

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


您可以使用该方法在线程池完成任务时暂停主线程。

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


您可以使用该方法在线程池完成其任务时暂停主线程。

调用
shutdown()
后,您可以使用阻止调用线程,直到所有任务都完成执行

由于超时,如果要等待任务完成所需的时间,则可以使用过大的值。但是,如果任务从未结束,则可能会使线程永远等待,因此,如果超时时间异常过长,则最好设置合理的超时时间,以便执行某些任务

例如:

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

调用
shutdown()
后,可以使用阻止调用线程,直到所有任务都完成执行

由于超时,如果要等待任务完成所需的时间,则可以使用过大的值。但是,如果任务从未结束,则可能会使线程永远等待,因此,如果超时时间异常过长,则最好设置合理的超时时间,以便执行某些任务

例如:

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

您还可以提交这些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();
List tasks=new ArrayList();
ExecutorService tp=Executors.newFixedThreadPool(10);
用于(最终表格信息表格:commandList){
ParallelExecutor pe=新的ParallelExecutor(表);
任务.添加(tp.提交(pe));
}
对于(未来p:任务){
p、 get();//带有超时p.get(10,TimeUnit.SECONDS);
}
tp.shutdown();

您也可以提交这些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();
List tasks=new ArrayList();
ExecutorService tp=Executors.newFixedThreadPool(10);
用于(最终表格信息表格:commandList){
ParallelExecutor pe=新的ParallelExecutor(表);
任务.添加(tp.提交(pe));
}
对于(未来p:任务){
p、 get();//带有超时p.get(10,TimeUnit.SECONDS);
}
tp.shutdown();

问题是,我正在运行的任务运行时间很长,我不确定这些任务何时才能完成。所以,我不能提供超时时间。@Angular初学者为什么不能?当然你会知道,他们将在不到10年的时间内完成。问题是,我正在运行的任务是长期运行的,我不确定这些任务将在何时完成。所以,我不能提供超时时间。@Angular初学者为什么不能?你肯定知道,他们将在不到10年内完成。