user6839132
user6839132

Reputation:

ThreadpoolExecutor and main thread executing in parallel

The thread pool executor is being executed in parallel to the main thread. The main thread is not waiting until the shutdown of the executor.

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

    }

I have used the while loop to keep the main thread waiting mean while the executor threads are in progress. But, it is not helping. Please let me know how to make the main thread wait.

while(!tp.isShutdown()){

                }

Upvotes: 1

Views: 2306

Answers (3)

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

After having called shutdown(), you can use awaitTermination(long timeout, TimeUnit unit) to block the calling thread until all tasks have completed execution.

As timeout you can use a value excessively big if you want to wait as long as it is needed for the tasks to complete however as it could make your thread wait forever if a task never ends, it is always better to set a reasonable timeout in order to execute some tasks if it is abnormally too long.

For example:

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

Upvotes: 3

You can also submit these Runnables and wait them to complete. It is also possible to specify a timeout to wait for the threads to execute before throwing an exception.

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

Upvotes: 1

Kayaman
Kayaman

Reputation: 73548

Of course it's not waiting. That's the whole idea of creating a threadpool, so your main thread can perform other tasks while the threadpool is executing additional tasks.

You can use the awaitTermination(long timeout, TimeUnit unit) method to have your main thread pause while the threadpool finishes its tasks.

Upvotes: 3

Related Questions