Results 1 to 5 of 5
  1. #1
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Serializing a Timer

    One of the classes that i need to serialize includes a few Timers. When I serialize it and then load it up again, the Timers are stopped. How do I start the timers where they left off?

    I'm using Swing Timers. Should I be using the other kind?

    I using the Timers in this case to change something after a countdown, but only once, then the Timers are stopped. I set the Timer tick interval to the time I want it to wait. Should I be using smaller tick intervals, and just waiting for the tenth (or so) tick, or is my way fine?

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Serializing a Timer

    Quote Originally Posted by Prime624 View Post
    One of the classes that i need to serialize includes a few Timers. When I serialize it and then load it up again, the Timers are stopped. How do I start the timers where they left off?

    I'm using Swing Timers. Should I be using the other kind?

    I using the Timers in this case to change something after a countdown, but only once, then the Timers are stopped. I set the Timer tick interval to the time I want it to wait. Should I be using smaller tick intervals, and just waiting for the tenth (or so) tick, or is my way fine?
    Record the amount of times your timer has ticked. Then after you serialize, set the timers interval to 1ms until you reach the recorded tick amount. Then restore the delay to normal. This is the only way I can think of

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: Serializing a Timer

    Well, there are several possibilities here. First, according to the API Doc for the java.swing.Timer class.

    "Warning: Serialized objects of this class will not be compatible with future Swing releases."

    The easiest is probably to simply record the duration of the timers along with a snap shot of the current time. Then when you serialize the timer, check the current time and compare to when the timer was started to see how much time has elapsed since you started the timer. Then you know at what value to set the timer when you reload it.

    You could use the java.util.Timer class and then schedule a task which simply counts down the event and then fires off the real task when it is time. This allows you to record the state and time remaining so you have more info to re-initialize your timer when you reload the class. This may not be efficient and could be problematic for short durations.

    Another possibility is to write your own timer class which saves sufficient state to do what you want. I have not looked at either Timer class code to see how the JDK does it but it might be useful.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Prime624 is offline Member
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    96
    Rep Power
    0

    Default Re: Serializing a Timer

    Quote Originally Posted by jim829 View Post
    Well, there are several possibilities here. First, according to the API Doc for the java.swing.Timer class.
    "Warning: Serialized objects of this class will not be compatible with future Swing releases."
    Regards,
    Jim
    So is this the reason it isn't working to begin with, or is this a separate issue? Would java.util.Timer work with serialization (if you know)?

    Thanks for the help to both of you.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,526
    Rep Power
    5

    Default Re: Serializing a Timer

    Quote Originally Posted by Prime624 View Post
    So is this the reason it isn't working to begin with, or is this a separate issue? Would java.util.Timer work with serialization (if you know)?

    I doubt it. It is just a warning that if you serialize the timer now, then in the future, your serialization methods may not work with future modifications of the class. The class behavior itself can't change because it is a published class. But internals which may be affected by serialization may change. At least, that's my guess.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  2. Serializing
    By shashanksworld in forum New To Java
    Replies: 7
    Last Post: 03-29-2010, 04:47 PM
  3. Object not serializing
    By MamboBanda in forum New To Java
    Replies: 1
    Last Post: 08-12-2008, 12:15 AM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. Serializing Arrays
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 09:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •