• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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: 23928
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: 23928
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
 
Lookout! Runaway whale! Hide behind this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic