how to count program running time

Discussion in 'Java' started by David, Oct 3, 2003.

  1. David

    David Guest

    user inputs several time points, after program starts, it will create a new
    thread everytime when reach a time point, every thread should append current
    running time with its output, result looks like this:

    java barbershop -c 1000 1500 2500 4000

    current time 0: barber is sleeping....
    current time 1000: customer 1 enter.
    current time 1000: barber starts cutting the hair of customer 1
    current time 1500: customer 2 enter.
    current time 2000: barber stopped cutting the hair of customer 1
    current time 2000: barber starts cutting the hair of customer 2
    current time 2500: customer 3 enter
    ........

    at first, i achieve this by record program start time: static final long
    startTime=System.currentTimeMillis();
    and then, every time when i need new time, i write: int
    currentTime=(int)(System.currentTimeMillis()-startTime).

    the problem is, when two users come too closely, program still performs the
    first user's routine but the second user arrival time has been pasted,
    therefore, second one will be ignored by the program, how to deal with that.

    some one suggests me use Timer, but i have no idea how to use it, can
    someone give me some hints, thanks
     
    David, Oct 3, 2003
    #1
    1. Advertising

  2. On Fri, 3 Oct 2003 18:53:08 +1200, David wrote:
    > the problem is, when two users come too closely, program still
    > performs the first user's routine but the second user arrival time
    > has been pasted, therefore, second one will be ignored by the
    > program, how to deal with that.


    My guess is that you are comparing the current time with a time from
    the list like this:

    if (currentTime == nextCustomer) {
    // start new customer thread;
    }

    So of course you might miss the time occasionally, especially if the
    customers are close together (but even sometimes when they aren't,
    depending on the granularity of System.currentTimeMillis() and the way
    you wait between customers).

    Instead, you should check like this to avoid missing any:

    if (currentTime >= nextCustomer) {
    // start new customer thread;
    }


    A timer isn't a bad idea though. Create one java.util.Timer object,
    then create and schedule a TimerTask for each customer. The TimerTask
    has a run() method that should create the customer thread (note that
    TimerTask.run() should not *be* the customer thread, it should just
    *start* the customer thread and then finish quickly).

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 3, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flamesrock
    Replies:
    8
    Views:
    502
    Hendrik van Rooyen
    Nov 24, 2006
  2. Replies:
    1
    Views:
    485
    Walter Roberson
    Aug 12, 2005
  3. Lorenzo Bettini
    Replies:
    3
    Views:
    632
    Lorenzo Bettini
    Sep 24, 2005
  4. Alexander Mahone

    Running a program (gperf) inside another C program

    Alexander Mahone, May 26, 2008, in forum: C Programming
    Replies:
    4
    Views:
    420
    Flash Gordon
    May 26, 2008
  5. efelnavarro09
    Replies:
    2
    Views:
    953
    efelnavarro09
    Jan 26, 2011
Loading...

Share This Page