This is the first time I post something in the beautiful and awesome StackOverflow community and my english is a bit rusty, but I'll try to explain the best I can.
I have the following situation:
In my main, I'm invoking a method through a TimerTask, because I need to schedule it so the method is executed every 5 seconds. Here is my main:
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
methodWithParams("HelloWorld");
}
};
timer.scheduleAtFixedRate(timerTask, 0, 5000);
}
And here is the invoked method:
public static void methodWithParams(String param){
System.out.println("Incoming Param: "+param);
}
With this, the output is like this every 5 seconds:
Incoming Param: HelloWorld
What I want is to run methodWithParams method every 5 seconds, but the first time that method is invoked, I can call it with some param but the rest of the time the param is anything else, so the result is something like this:
Incoming Param: HelloWorld
Incoming Param: HelloWorld2
Incoming Param: HelloWorld2
Incoming Param: HelloWorld2
How can I do that? Any suggests?
Thank you very much in advance!!
I found a solution to my problem and I deleted the post (my fault). I undeleted so the solution can be available for someone who has the same question I had.
What I did was this: By creating a boolean class var inside the task, I was able to control if it was the first execution or not since the task behaves like a loop.
public static void main(String[] args) {
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
boolean first = true;
@Override
public void run() {
if(first){
first = false;
methodWithParams("HelloWorld");
}else{
methodWithParams("HelloWorld2");
}
}
};
timer.scheduleAtFixedRate(timerTask, 0, 5000);
}