multithreading - How to pass dynamic Periodic interval from runnable task to Java : ScheduledExecutorService - Stack Overflow

admin2025-04-25  3

Job: Need to invoke API periodically with time interval. Interval is determined from API only because API will return expireIn as a interval and We need to invoke the API again with that interval.

Purpose of Interval: Approaching API for the Token. And Token will be expired on particular timing. Expire timing will be also returned by API along with token. Need to invoke API for getting fresh token based on interval.

Approach tried & failed: Invoking API to get the response of token & expire timing as runnable task with ScheduledExecutorService in method of scheduleWithFixedDelay().

Approach failed: Unable to pass interval of expire of API response from runnable task to the method of scheduleWithFixedDelay(Runnable task, initialdelay, interval, Time unit);

From below code, I tried to declare variable interval as volatile to maintain single main memory. But can not pass it. scheduleWithFixedDelay() accpeting the initial value of volatile variable interval, not with the interval getting from API response method. Kindly help to achieve it. I tried with Thread.sleep() but unable to handle thread memory properly. Let me know if any other best approach for the job.

Code:

ScheduledExecutorService scheduledExecutorService =  Executors.newSingleThreadScheduledExecutor();
            scheduledExecutorService.scheduleWithFixedDelay(
                    new Runnable() {            
                        public void run() {
                            try {
                                interval = RestClient.getResponse();
                            } catch (Exception e) {
                                printErrorLog(e);
                            }
                        }
                    }, 
                    0, 
                    interval, 
                    TimeUnit.SECONDS);  

Job: Need to invoke API periodically with time interval. Interval is determined from API only because API will return expireIn as a interval and We need to invoke the API again with that interval.

Purpose of Interval: Approaching API for the Token. And Token will be expired on particular timing. Expire timing will be also returned by API along with token. Need to invoke API for getting fresh token based on interval.

Approach tried & failed: Invoking API to get the response of token & expire timing as runnable task with ScheduledExecutorService in method of scheduleWithFixedDelay().

Approach failed: Unable to pass interval of expire of API response from runnable task to the method of scheduleWithFixedDelay(Runnable task, initialdelay, interval, Time unit);

From below code, I tried to declare variable interval as volatile to maintain single main memory. But can not pass it. scheduleWithFixedDelay() accpeting the initial value of volatile variable interval, not with the interval getting from API response method. Kindly help to achieve it. I tried with Thread.sleep() but unable to handle thread memory properly. Let me know if any other best approach for the job.

Code:

ScheduledExecutorService scheduledExecutorService =  Executors.newSingleThreadScheduledExecutor();
            scheduledExecutorService.scheduleWithFixedDelay(
                    new Runnable() {            
                        public void run() {
                            try {
                                interval = RestClient.getResponse();
                            } catch (Exception e) {
                                printErrorLog(e);
                            }
                        }
                    }, 
                    0, 
                    interval, 
                    TimeUnit.SECONDS);  
Share Improve this question asked Jan 16 at 13:43 anand elsonanand elson 211 silver badge2 bronze badges 3
  • Try using java.util.Timer.schedule(Timertask task, long delay) – John Williams Commented Jan 16 at 16:29
  • But how to pass periodic delay time getting from task to schedule method. Can you brief it. – anand elson Commented Jan 16 at 18:57
  • Start with @June's answer below. – John Williams Commented Jan 17 at 12:14
Add a comment  | 

1 Answer 1

Reset to default 1

Your task is a Delayed Task, not a Periodic Task, which means it should be executed after some time, rather than being executed periodically. You should use schedule instead of scheduleAtFixedRate or scheduleWithFixedDelay.

ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
Runnable freshTask = new Runnable() {
    @Override
    public void run() {
        try {
            long interval = RestClient.getResponse();
            scheduledExecutorService.schedule(this, interval, TimeUnit.SECONDS);
        } catch(Exception e) {
            printErrorLog(e);
        }
    }
};
scheduledExecutorService.execute(freshTask); // initiate the chain
... 
// remember to stop the loop
scheduledExecutorService.shutdown();
转载请注明原文地址:http://anycun.com/QandA/1745529251a90809.html