title: How to keep main thread in waiting state after spawn multiple async jobs in spring batch
body: <p>I have configured async job launcher using TaskExecutor in spring batch.</p>
  
  <p>I have launched n async jobs from main thread.</p>
  
  <p>Now I want to keep main method wait till all n jobs get complete. I have tried to use CountDownLatch and Phaser but could not achieve. </p>
  
  <p>Please suggest how to achieve it?  </p>
  
  <p>Here are some similar dummy code fragments.</p>
  
  <pre><code>@Configuration
  public class SpringBatchConfiguration { 
      @Bean
      public TaskExecutor taskExecutor() {
          ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
          taskExecutor.setMaxPoolSize(maxThreadCount);
          taskExecutor.afterPropertiesSet();
          return taskExecutor;
      }
  
      @Bean
      @Primary
      public JobLauncher getJobLauncher(JobRepository jobRepo, SimpleAsyncTaskExecutor taskExecuter) {
          SimpleJobLauncher jobL = new SimpleJobLauncher();
          jobL.setJobRepository(jobRepo);
          jobL.setTaskExecutor(taskExecuter);
          return jobL;
      }
  }
  </code></pre>
  
  <p>Main thread executing below code.</p>
  
  <pre><code> Class JobHandler {
      @Autowired
      JobLauncher jobLauncher;
  
      // Main thread executing demo method
  
      public void demo () {
          launchJob(job1, builder1);
          launchJob(job2, builder2);
          launchJob(job3, builder3);
          launchJob(job4, builder4);
          launchJob(job5, builder5);
          launchJob(job6, builder6);
  
          // Here I want to do operation when all six jobs complete successfully
          // Please suggest how to do.
      }
  
      public void launchJob(Job job, JobParametersBuilder builder ) { 
          try {
  
              // Asynchronous job submission  
              final JobExecution execution = jobLauncher.run(job, builder.toJobParameters());     
          }
          catch(Throwable th) {
              log.error("Job execution failed");
          }       
      }
  }
  </code></pre>