Search code examples
javamultithreadingthread-synchronization

scheduling a periodic task after other threads finish


I want to have 3 threads doing a task concurrently and also I want a thread that do a scheduled task over a period of time (runs every 10 secs). But I want when the 3 threads finish to run only once the scheduled task and then I want this thread to terminate. What is the best way to implement these threads? Is the ExecutorService Interface suitable for this.


Solution

  • Here an example that I've just written:

    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledFuture;
    import java.util.concurrent.TimeUnit;
    
    public class Example {
    
        public static void main(String[] args) throws InterruptedException {
            ScheduledFuture future = Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new MyScheduledTask(), 0, 10, TimeUnit.SECONDS);
            ExecutorService executor = Executors.newFixedThreadPool(3); // pool composed of 3 threads
            for (int i = 0; i < 3; i++) {
                // for the example assuming that the 3 threads execute the same task.
                executor.execute(new AnyTask()); 
            }
            // This will make the executor accept no new threads
            // and finish all existing threads in the queue
            executor.shutdown();
            // expect current thread to wait for the ending of the 3 threads
            executor.awaitTermination(Long.MAX_VALUE, TimeUnit.TimeUnit.NANOSECONDS); 
            future.cancel(false); // to exit properly the scheduled task
            // reprocessing the scheduled task only one time as expected
            new Thread(new ScheduledTask()).start(); 
    
        }
    
    
    }
    
    class MyScheduledTask implements Runnable {
        @Override
        public void run() {
            //Process scheduled task here
        }
    }
    
    class AnyTask implements Runnable {
        @Override
        public void run() {
            //Process job of threads here
        }
    }