aspose file tools
The moose likes Java in General and the fly likes how to use System.currentTimeMillis() to check time for every 10 milli seconds? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "how to use System.currentTimeMillis() to check time for every 10 milli seconds?" Watch "how to use System.currentTimeMillis() to check time for every 10 milli seconds?" New topic
Author

how to use System.currentTimeMillis() to check time for every 10 milli seconds?

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
how to use System.currentTimeMillis() to check time for every 10 milli seconds?


Abhishek
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56207
    
  13

Please reserve this forum for questions an advanced Java. I have moved this most to a more appropriate forum.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4422
    
    2

I wouldn't do it with that call.
Just call Thread.sleep(10);
Or if needed, put the Thread.sleep() in a thread, and block for a semaphore.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
I'm not sure we understand what you're trying to do. Check time for what? Is there some other task you want to do every 10 milliseconds?

If that's the case, I would probably use a java.util.Timer or a java.util.concurrent.ScheduledExecutorService. Or possibly Quartz if the timing is critical and results from the other two techniques aren't exact enough.


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
What's the resolution of modern PC clocks? Not too long ago, I think you couldn't get two system time values 10 millis apart because the clock didn't update that often. What do you have to do 100 times a second? Are you sure you can finish within 10 milliseconds, before the next one starts?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks for the reply..

i want to do some activity after every 10 milli seconds....
i was assigned a task on threads where i have to do some activity after every 10 milli seconds.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4422
    
    2

Originally posted by Abhishek Reddy Chepyala:

i want to do some activity after every 10 milli seconds....
i was assigned a task on threads where i have to do some activity after every 10 milli seconds.


Then create a thread to doTheThing() and in the thread's main loop,
have something like:

[ October 20, 2007: Message edited by: Pat Farrell ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
That will pause 10 millis between reps. If the task takes 5 it will run every 15. Depending on requirements, that might be perfectly acceptable but it won't trigger 100 times a minute. Look at the how java.util.Timer can trigger 10 millis after the last run triggered or 10 millis after the last run finished.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4422
    
    2

yes, my code fragment assumes you doTheThing() quickly.

If it takes a long time, you will start them every 15 or more millis.
Worse, if it takes a really long time, you might really want separate execution threads.
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
Doesn�t warranties that your task will be executed in exactly 10 milliseconds. It will awaken in 10 milliseconds and will be added to a queue, it will still have to wait till it comes it�s turn to be the thread in execution.

I think it is better option to use a timer.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
A Timer doesn't guarantee exactly 10 milliseconds either. According to the API: "This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method." The API for wait() says it waits until "...the specified amount of real time has elapsed, more or less". I don't think it's any more accurate than Thread.sleep(). As Stan correctly pointed out, on many PCs you can't get timings more precise than 10 milliseconds anyway. I recommended Timer because it handles fixed rates where the task time is nonzero more easily than Pat's code does. Not that that's terribly complex to handle, but my point is the work's already done for you. However it's no better than sleep() if you want exact timings - that simply can't be guaranteed.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[message edit; let's move this to threads]

Well, like many of them pointed out, one is not likely to get anything down to ten millisecond interval on a reliable clock tick in Java without some adroit analysis of exactly what it is you want to do.

What you have posed is an inversive recursion, check the clock every ten milliseconds. Pat's reply is the most reliable, but without some concept from you, we will have a hornet's nest in about 12 milliseconds every ten millis seconds, will you be here to help us swat the hornets ?

I did not try to compile this code, you will have to provide us more detail, there are plenety of posters here who know the toolset in detail.


[ October 22, 2007: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to use System.currentTimeMillis() to check time for every 10 milli seconds?
 
Similar Threads
regarding strings
a question
Difference between '+' and concat in string
Java time with servlet in-accurate
time pattern for milli second