• Post Reply Bookmark Topic Watch Topic
  • New Topic

scheduleWithFixedDelay task nitialization isssue  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • 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) {

}
}
};
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 1489
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.

http://www.coderanch.com/t/658758/java/java/final-Runnable

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!