Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to count program running time

Reply
Thread Tools

how to count program running time

 
 
David
Guest
Posts: n/a
 
      10-03-2003
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


 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-03-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Count = Count + 1 Using only std_logic_1164 Doubt efelnavarro09 VHDL 2 01-26-2011 03:49 AM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
Help !I want to write a program to count the running time of another program freehomesp@yahoo.com.cn C Programming 1 08-12-2005 06:13 AM
Count(*) in a Subquery with multiple tables: How does SQL determine which table to generate the Count() from? Kaimuri MCSD 3 12-29-2004 06:38 PM
I am adding a new row to the datagrid dynamically but if i use the Count property of Item it is not showing the count of the new rows being added Praveen Balanagendra via .NET 247 ASP .Net 2 06-06-2004 07:16 AM



Advertisments