This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Time" Watch "Time" New topic
Author

Time

Ole Kristian
Greenhorn

Joined: Jul 02, 2001
Posts: 7
Hi!
I need som help:
I need a timer that start from zero and then goes to infinity(or close..). It has to be a real timer. The point is to compare that timer to a time in an instance( that I have made), so when these to times is equal my program can continue as normal ( more like a loop-thing) :
while(timeInIntance<=timer)
{
//wait
}
//then the show goes on
I can't see that the Timer-class or the Time-class in the java have this functionality.

Any bright ideas?


Ole Kristian
Tom Pelly
Greenhorn

Joined: Jun 29, 2001
Posts: 14
Try:
long i = System.currentTimeMillis()
That should be what you're looking for.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
And waiting until the specified time is easy:
<code><pre>
public void waitUntil(long resumeTime) {
long currentTime = System.currentTimeMillis();
try {
Thread.sleep(resumeTime - currentTime);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
</pre></code>

[This message has been edited by Jim Yingst (edited July 02, 2001).]


"I'm not back." - Bill Harding, Twister
Ole Kristian
Greenhorn

Joined: Jul 02, 2001
Posts: 7
Sorry, not quite what I'm looking for...The time MUST start at 0 so to speak. That's because I want to trigger a event already from 2 or 3 milliseconds ( or in fact even 1 millisecond).
And the System.currentTimeMillis() returns the present time.
The time in the instances is always past, so I can't compare it to the current time in the computer.
Do you have other ideas?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Ummm... let's put it this way. If you had a watch with a second hand, which always displayed the current time, with no option to reset the watch to zero - do you think you might possibly be able to use such a watch to measure an interval such as, say, 5 seconds? Would it really be necessary to "start at zero", or might there be a phenomenally simple way of working around this?
Ole Kristian
Greenhorn

Joined: Jul 02, 2001
Posts: 7
It's time for some background info:
I have a class in my program. The class contains information on let's say a plane(coordinates, etc). This class(or severeal similar classes--> updates on the coordinates to this plane) is sent from a client to a server (With streams, readObject and writeObject-methods). When the class arrives at the server, the program sets a field in the class with the computer's "System.currentTimeMillis()"-method. Then the class is written to a file (the class is Serializable). So when I opens that file and want to read from it, the problems arrives...
Then I want to recreate the past so to speak
I want to send this class-instances in the same speed that I received them at the server! And that's why I need a "stopwatch" (which starts at zero) so I can program some algorithm to send the instances with the same amount of time between as when they arrived at the server.My idea is that I can calculate the time between the first instance arrived ( startTime) and the time between every updates, so I get what I call timeDifference ( whic goes from 0 --> infinity).
EX:
instance1(timedifference between startTime and instance1):
0 milliseconds
instance2 (timedifference between startTime and instance2) :
3 milliseconds
instance3 (timedifference between startTime and instance3) :
5 milliseconds
.
.
etc.
With this timedifference I can calculate when the instance should be sent if the stopwatch starts at zero.
I hope I confused you a lot with this explenation
Tom Pelly
Greenhorn

Joined: Jun 29, 2001
Posts: 14
I don't think I quite followed all of that, but still can't see why you need to start at zero.
If you *really* need to start at zero, can't you just minus the initial time reading off all subsequent readings to give a variable that acts as if it started at zero?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Zero is a very arbitrary thing.

I can recall a job I once had where we'd mix together exacting amounts of material (sand, clay, regolith, etc) and then add enough water to make it exactly 10 kg.

While we could have used 10 separate containers, or 10 separate scales, we instead used 1 container, 1 scale, and a cool little button on the scale labelled "tare".

Which means "re-zero it".

Zero time is when *you* decide it is. The instant you start your stopwatch would be on your first instance.

Then I'd rewrite your EX: to be like this:
Every other 'tick' would be separated from this 'zero' time, by a certain amount of milliseconds between 'the last' and 'the current'
instance1(timedifference between "nothing: it's the first one" and instance1):
0 milliseconds
instance2 (timedifference between instance1 and instance2) :
3 milliseconds
instance3 (timedifference between instance2 and instance3) :
5 milliseconds

Is this not a possible way to implement what you're wanting?
Ole Kristian
Greenhorn

Joined: Jul 02, 2001
Posts: 7
Well, I have tried a different aproach in the last few days. Instead of having a stopwatch or a counter I have used a Timer. And I have set a new delay (which represent the timedifference between to "packages" (again; these packages contains information about f.ex an airplane. It's coordinates, speed, etc. This information is used when the airplane is animated on a map in my application) so that this delay animates the real timedifference over network. The whole point is to make my scenario( which is loaded from a file) go as fast as the same scenario over network. But that was a failure. The timer used too much time to go to sleep and wake up again so my scenario (from file) went slower by a factor of 2,7 :-( So that's why I need a "stopwatch" so I can compare time from that stopwatch with the time in the packages and send the packages to animation with a minimum of delay.
Has anybody som experience with this sort of problem?
Best Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Time
 
Similar Threads
Timer
problem in when to use this in java
Problem with Timer and TimerTask
Stuck on setVisible()
Auto-submit <form> to a servlet.