public class ThreadDemo3 {
public static void main(String[] args) {
final int index = 10;
final String[] strs = new String[10];
for (int i = 0; i < index; i++) {
strs[i] = i + " data ";
}
final Timer timer = new Timer();
TimerTask task=new TimerTask() {
int i=0;
public void run() {
for (int j = 0; j < 2; j++) {
System.out.println(strs[i]);
i++;
if(i==index)
timer.cancel();
}
System.out.println("--------------");
}
};
timer.scheduleAtFixedRate(task, 500, 2000);
}
} |