Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to use System.currentTimeMillis() to check time for every 10 milli seconds?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please reserve this forum for questions an advanced Java. I have moved this most to a more appropriate forum.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!