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: Thread/timer problem

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Thread/timer problem

    Ok, so basically I have a program that's simulating something and occasionally I want to start a timer that will create a new thread, wait for the specified amount of time, then send a notification back in the original program.
    The problem I'm having is that the timer will start and go off, but not after the correct specified time... Here's my code to make it clearer:
    public class NewTimer {
    Timer timer;
    static int current_num;
    static long start_time;
     
    public NewTimer(long milliseconds, int curr_num) {
        current_num =curr_num;
        timer = new Timer();
        timer.schedule(new RemindTask(), milliseconds);
        start_time = System.currentTimeMillis();
    }
     
    class RemindTask extends TimerTask {
        public void run() {
            System.out.println("Time Elapsed: " + (System.currentTimeMillis() - start_time));
     
            timer.cancel();
        }}}
    I'm calling it from another class with the following code:

    new NewTimer(temp_time, current_num);

    The problem is that the "Time Elapsed" is often significantly (2-3x) bigger than the time sent into the timer. I'm at my wits end trying to figure out why and I feel it must be a misunderstanding on my part about threads or something. When I run this program by itself it seems to work (with it's own main, etc.), but when combined with my other program it doesn't.
    This should be it's own independent thread should it not? Why would stuff going on in the other threads in my program affect this?
    Any help is appreciated.. this is killing me.
    thanks
    Last edited by korbal; March 18th, 2010 at 05:54 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Thread/timer problem

    I think it probably has something with the fact that current_num and start_time are declared static.

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

    korbal (March 18th, 2010)

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. bubble sort timer problem
    By JavaNoob82 in forum Algorithms & Recursion
    Replies: 1
    Last Post: March 12th, 2010, 09:22 AM
  3. Timer Class help
    By Deadbob in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 23rd, 2010, 12:18 AM
  4. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM
  5. Swing problem, newbie'ish . Perhaps thread related?
    By fenderman in forum AWT / Java Swing
    Replies: 3
    Last Post: July 22nd, 2009, 04:45 AM