title: Scheduling Concurrent Jobs in Java
body: <p>I have implemented Schedulers in java in my code using 2 different ways. However, both seem to work in sequential rather than parallel thread. mine is a spring 4 application on java 8.</p>
  
  <p>first way</p>
  
  <pre><code>@Scheduled(cron = "0/59 * * * * *")
  public void test2 (){
  </code></pre>
  
  <p>second way</p>
  
  <pre><code> @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
      try{
  
          taskRegistrar.addTriggerTask(
                  new Runnable() {
                      @Override public void run() {
                          try {
  
                              System.out.println("some configure task running every 10 seconds --" + new Date());
                          } catch (Exception e) {
  
                          }
                      }
                  },
                  new Trigger() {
                      @Override public Date nextExecutionTime(TriggerContext triggerContext) {
                          Calendar nextExecutionTime =  Calendar.getInstance();
  
                          Date lastActualExecutionTime = triggerContext.lastActualExecutionTime();
                          nextExecutionTime.setTime(lastActualExecutionTime != null ? lastActualExecutionTime : new Date());
  
                          nextExecutionTime.add(Calendar.SECOND, 1000); 
                          return nextExecutionTime.getTime();
                      }
                  }
          );
  </code></pre>
  
  <p>both are not working in concurrent mode. this is huge inconsistency in the jobs. like for example i want a job to run every 100 seconds, is never starts at every 100 seconds. it could be a delay of more than 3-4 mins as well depending upon completion of previous job.
  can somebody please point to a better solution. thanks</p>
  
  <p>i tried the following as was suggested below.</p>
  
  <pre><code>ConcurrentTaskScheduler scheduler =  new ConcurrentTaskScheduler();
      scheduler.schedule(new Runnable() {
          @Override
          public void run() {
              System.out.println(new Date() +"start of test2 crons job running every 59 seconds test2 test2 test2");
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
              System.out.println(new Date() +"end of test2 crons job running every 59 seconds end end end ");
          }
      }, new CronTrigger("0/30 * * * * *"));
  </code></pre>
  
  <p>i created 5 such jobs and ran them but they are still running sequential. i am not able to find my mistake here or if somebody has any code snippet or any link where i can find any sample code would be very helpful. </p>