简体   繁体   中英

Using ScheduledExecutorService, How to Start a Thread without waiting for other thread to complete at fixed interval?

I want to run a task at every particular interval of time regardless of completion of previous thread. And I've used ScheduledExecutorService with the schedule time at every one second. But the problem is, in my Runnable, If I make thread to sleep for 5 seconds, My ScheduledExecuterService also getting executed in every 5 seconds while it supposed to run each thread at 1 second.

It seems like it ScheduledExecuterService is waiting for previous thread to completion. But I want, The task to be triggered at every 1 second no matter what if job inside the task waits for longer time.

Here's is my code.

public class MyTask implements Runnable {

   public void run() {
       System.out.println("hi there at: "+ new java.util.Date());
       try {
           Thread.sleep(5000);
       } catch (InterruptedException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
   }
}

And here's my ScheduledExecutorService Code.

public class JavaScheduledExecutorServiceExample {

   public static void main(String[] args) {
       ScheduledExecutorService execService = Executors.newScheduledThreadPool(5);
       execService.scheduleAtFixedRate(new MyTask(), 0, 1000, TimeUnit.MILLISECONDS);
   }
}

Correct me If I'm doing something wrong. And If I'm wrong, is there any alternative to achieve the same? Providing Any best practices could be more helpful :)

" If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute. " The behavior you are seeing is consistent with the javadocs

I believe this will perform the way you specified:

public class JavaScheduledExecutorServiceExample {
    private static ScheduledExecutorService execService = null;
    private static int timesAsleep = 0;

    public static class MyTask implements Runnable {

        public void run() {
            System.out.println("hi there at: "+ new java.util.Date());
            // schedule again
            execService.schedule(new MyTask(), 1000, TimeUnit.MILLISECONDS);
            try {
                int i = timesAsleep;
                timesAsleep++;
                System.out.println("asleep " + i + "----------------------");
                Thread.sleep(5000);
                System.out.println("awoke " + i + "----------------------");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        execService = Executors.newScheduledThreadPool(5);
        execService.schedule(new MyTask(), 1000, TimeUnit.MILLISECONDS);
    }

}

Notice the use schedule() instead of scheduleAtFixedRate() on the ScheduledExecutorService instance. It also schedules the next task as soon as it starts the new task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM