I am making a program that shows cellular growth through an array. I have gotten it so when I press the start button, the array updates every 10 seconds in a while(true){} l
I personally prefer using the Timer class rather than Thread
or Thread.sleep()
. The timer class handles both running the code periodically and canceling it.
Your code would look like:
TimerTask myTask = new TimerTask() {
public void run() {
// here goes my code
}
};
Timer timer = new Timer();
timer.schedule(myTask, 0 /*starts now*/, 10 * 1000 /* every 10 seconds */);
// whenever you need to cancel it:
timer.cancel();