I know I am probably way off here, but I am trying to create a timer array so that mytimer[0] mytimer[1], mytimer[2], etc… all fire off at a different interval, with diffrerent events sent to a server. Any ideas? The for loop value of 6 is an organic number for testing purposes only. This number would later be decided base on a setting from the program’s xml file.
Timer mytimers[] = new Timer[6];
for(int i = 0;i < 6;i++){
final int mytime = i;
mytimers[i].scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//do action
sendData("Timer " + mytime + " fired");
}
}, 10000, i*1000);
}
I’m assuming this is the line that doesn’t work? You can’t initialize an array with an object; initialize it with an array:
Making another guess, you’re also not initializing the individual timers:
At this point mytimers[i] isn’t set to anything, so how can you call
scheduleAtFixedRateon it? Initialize it first:EDIT:
Your “IllegalArgumentException: Non-positive period.” is because on the first time through the loop,
i = 0, soi * 1000 = 0, and the period can’t be 0 (“run this event every 0 zero seconds”).Start with
i = 1and it should be fine.