6 Replies Latest reply: Oct 7, 2008 4:25 AM by 843785 RSS

    completely destroying instances or stopping threads

    843785
      Again with my DateTimePicker, I discovered that if you press "OK" without selecting a date, it re-creates the date time picker, but the thread from the previouds instance is not "destroyed", so it updates the text fields with the previous instance's time values. Is there any known way to totally get rid of previous instances? (System.gc doesn't work)



      Azriel~
        • 1. Re: completely destroying instances or stopping threads
          791266
          What DateTimePicker? Did you implement it?
          • 2. Re: completely destroying instances or stopping threads
            843785
            What DateTimePicker?
            wrote one myself.

            Screenie:
            http://i37.tinypic.com/1q4ifb.png

            If you click the OK button without the table with the dates having focus on any number, it will have two "timeThread"s running to update the clock - one that was running before you click OK, one that is created from the new instance of the clock after the error message asking you to choose a date. The extra problem is that the time that is updating from the previous timeThread is from the previous intance of the clock (i.e. if you set the previous clock time to be 8:03:52 pm, that's what the first timeThread will try to tell the clock (and JTextFields) to display on the screen. The the 2nd timeThread tells the same things the new time (that it got when the clock is re-created). so... we have two threads running (in which we only want the 2nd one). The problem again: how do we get rid of the first one?

            Azriel~
            • 3. Re: completely destroying instances or stopping threads
              791266
              Azriel-- wrote:
              What DateTimePicker?
              wrote one myself.
              Ok. You should in that case fix your code. Your thread should notify your thread that it should stop executing when the user clicks ok.

              Kaj
              • 4. Re: completely destroying instances or stopping threads
                843785
                oh I've tried. I've tried
                while(Thread.currentThread() == timeThread){
                Thread.yield();
                return;
                }
                //after this it re-creates the Clock instance, so timeThread is reassigned with the new instance
                I've tried the depreciated methods as well (stop(), destroy()) and I've tried making timeThread = null; but to no avail. Btw, is there a DateTimePicker class that can be implemented? (oh my goodness if there is, because all I could find on the net was that lavantech one)


                Azriel~
                • 5. Re: completely destroying instances or stopping threads
                  791266
                  Azriel-- wrote:
                  oh I've tried. I've tried
                  while(Thread.currentThread() == timeThread){
                  Thread.yield();
                  return;
                  }
                  //after this it re-creates the Clock instance, so timeThread is reassigned with the new instance
                  O_o

                  What do you expect that to do?
                  • 6. Re: completely destroying instances or stopping threads
                    843785
                    lol dunno. My mind had its limits.

                    On another note, I've "fixed" it by taking the timeThread outside of the Clock class (by putting it into the enclosing class), so the Clock instance is reassigned to it without making another instance of timeThread. [solved] unless you'd like to think of another (most probably better) solution.


                    Azriel~