• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

scheduleWithFixedDelay task nitialization isssue

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It gives variable runnableTask might not have been initialized.


public void scheduler() {

scheduler = Executors.newScheduledThreadPool(1);

final Runnable runnableTask = new Runnable() {
@Override
public void run() {
try {
future = scheduler.scheduleWithFixedDelay(runnableTask , 10, 10, TimeUnit.SECONDS);

} catch(Exception ex) {

}
}
};
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kri shan wrote:It gives variable runnableTask might not have been initialized.



This code is confusing. You are creating a runnable, which if it ever gets scheduled, would schedule itself to be run in the future. And since you never actually go through a code path that actually schedules the runnable, it never gets a chance to run the code that schedules itself.

Henry
 
kri shan
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This one runs fine with scheduler interval 'x' all the time. How to change different scheduler interval 'x' during run time? future = scheduler.scheduleWithFixedDelay(runnableTask , x, x, TimeUnit.SECONDS) called only first time and run() method runs every time based on same scheduler interval 'x'.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kri shan wrote:
This one runs fine with scheduler interval 'x' all the time. How to change different scheduler interval 'x' during run time? future = scheduler.scheduleWithFixedDelay(runnableTask , x, x, TimeUnit.SECONDS) called only first time and run() method runs every time based on same scheduler interval 'x'.



If the scheduleWithXXX() versions of the methods doesn't provide the correct scheduling, unfortunately, you will have to schedule it yourself. You can only schedule one execution, and find a way to schedule the other executions... and of course, if you want one execution to schedule the next one, then don't do the silly thing described in your other topic.

https://coderanch.com/t/658758/java/java/final-Runnable

Henry
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic