1

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

David
  • 15,652
  • 26
  • 115
  • 156
  • What will you use the list of threads for? – aioobe May 26 '11 at 12:09
  • 1
    obviously the answers from your previous post have been helpful - please go back to that question and upvote helpful answer/accept the one that helped you the most. That's how we say "thanks" on Stackoverflow :) – Andreas Dolk May 26 '11 at 12:18
  • You should not start a thread in a constructor, in general. See the reference to the Goetz article, [here](http://stackoverflow.com/questions/84285/calling-thread-start-within-its-own-constructor). I don't think it's a problem in your case, but I'm not 100% sure. – toto2 May 26 '11 at 13:15

3 Answers3

2

I need to do this with some kind of dynamic array

Try using an ArrayList.

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

Something like this should do:

  1. Create a List to store the counters:

    List<Counter> myCounters = new ArrayList<Counter>();
    
  2. Add new counter-threads like this:

    int nexti = myCounters.size();
    myCounters.add(new Counter(nexti));
    
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 1
    Please forgive my limiyed java experience, but why does the OP need to keep a reference to the threads at all? Nothing is done with them, so why keep them? Is this some garbage-collection avoidance strategy? – Martin James May 26 '11 at 12:21
  • @Martin James. I don't know. That's why I posted a comment below the question. – aioobe May 26 '11 at 12:23
  • Thank you :) PS: I needed to keep a reference to the threads, I am going to be adding a lot to that class. – David May 26 '11 at 12:47
1

Create a storage list

List<Counter> lst = new ArrayList<Counter>();

add thread to list, on click

Counter counter = new Counter(someInt);
lst.add(counter );

Also try to manage deleting reference of thread from List

jmj
  • 237,923
  • 42
  • 401
  • 438
  • The OP is already creating a thread inside the constructor of `Counter`. No need to create two threads per `Counter`. – aioobe May 26 '11 at 12:25
0

I would definitely not be creating a new Thread every time the user clicked a button. If they click it a few hundred times your application might die or slow down. When they click the button you know what time you want your Timer to expire. So rather create a Timer object and put that on an ordered queue (ordered from earliest to latest expiry time) and then have a single Thread monitoring that queue, popping off Timers as they expire.

Your TimerWatcher thread can then wake up every few milliseconds, or better, wait until it knows the first Timer is going to expire and then peak at the next Timer and wait until this next one expires. Whenever you add a new Timer you can also wake up your TimerWatcher and check that the new Timer has not expired to that it doesn't expire before the one you were already waiting for.

alpian
  • 4,668
  • 1
  • 18
  • 19
  • I don't think the app will die because of a few hundred threads. Otherwise, it's a good answer :-) – aioobe May 26 '11 at 12:06
  • @aioobe: You're probably right but a Thread just to sit there and sleep seems like a waste to me. :) – alpian May 26 '11 at 12:07
  • Having a single thread sounds cool, it would make the program more efficient, and I like efficient code :) It's more complicated though, and speed is not much of an issue at this stage. I'll keep it in mind though, it's a good idea. – David May 26 '11 at 12:52