Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Stopping All Current Thread

  1. #1
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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();
    comp.add(ball);
    Runnable run = new BallRunnable(ball, comp);
    t = new Thread(run);
    t.start();

    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.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  3. The Following User Says Thank You to copeg For This Useful Post:

    newbie (November 27th, 2010)

Similar Threads

  1. While loop stopping?
    By Echo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 16th, 2010, 05:40 PM
  2. Code stopping, need help fast.
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2010, 09:00 AM
  3. how to add a new entry in a current array
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: December 28th, 2009, 06:39 PM
  4. Program to print current directory path to the console
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 9th, 2009, 12:59 PM
  5. How to Get the current date and time
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 2nd, 2008, 01:55 PM