I have a button, that when pressed, should create an instance of a new thread: a Countdown Timer. The problem is, I need to do this with some kind of dynamic array, because there is no way of knowing how many times the user will press the button!
This is the code in the button's action listener:
Counter c = new Counter(timeToFinish);
This is the code for the Counter class:
class Counter implements Runnable {
int waitingTime = 0;
Thread myCounter = new Thread(this);
public Counter(int waitingTime)
{
this.waitingTime = waitingTime;
myCounter.start();
}
public void run(){
//Start countdown:
do
{
waitingTime -= 1;
try {
Thread.sleep(1000);
System.out.println(waitingTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (waitingTime >= 0);
}
}
If the user presses the button ten times, ten instances should be created, from c(0) to c(9), each separate threads. I don't know how to create a dynamic array of threads :S