I use ScheduledExecutorService and I want it to do some calculation every 10 seconds for a minute and after that minute to return me the new value.How do I do that?
Example: So it receives 5 it adds +1 six times then it should return me after a minute the value of 11.
What i have so far but is not working is:
package com.example.TaxiCabs;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.*;
public class WorkingWithTimeActivity {
public int myNr;
public WorkingWithTimeActivity(int nr){
myNr = nr;
}
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(1);
public int doMathForAMinute() {
final Runnable math = new Runnable() {
public void run() {
myNr++;
}
};
final ScheduledFuture<?> mathHandle =
scheduler.scheduleAtFixedRate(math, 10, 10, SECONDS);
scheduler.schedule(
new Runnable() {
public void run() {
mathHandle.cancel(true);
}
}, 60, SECONDS);
return myNr;
}
}
and in my main activity and i want it after 1 minute to change my txtview text into 11;
WorkingWithTimeActivity test = new WorkingWithTimeActivity(5);
txtview.setText(String.valueOf(test.doMathForAMinute()));