Stopping All Current Thread
Program: Bouncing Ball - Each click of button adds a moving ball onto a component
Goal: To stop all threads when i click on a stop button
When go button is clicked, the following is performed:
Bounce ball = new Bounce();
Runnable run = new BallRunnable(ball, comp);
t = new Thread(run);
When user clicks go again, it will add the new ball. I've only allowed 10 balls to be placed, as my button will Gray-out after 10 clicks.
I've tried invoking .interrupt on 't', but only stops ONE ball on the screen because i only use one instance of Thread throughout.
Could anyone please shed light on how it would be possible to stop and clear ALL of the balls on the screen, not just one.
My initial thought it to possibly implement a generic ArrayList of type Thread, but not sure how to go about it.
Thanks a bunch for your time.
Re: Stopping All Current Thread
You could do this a number of ways. Keep references to the threads (for example in a List) and iterate through each to stop each one. Alternatively, keep a single boolean flag which is checked in each thread during its operation, keeps going if true or exits if false - change this flag to false if you want them to exit.