• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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

 
Ranch Hand
Posts: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to use System.currentTimeMillis() to check time for every 10 milli seconds?
 
Sheriff
Posts: 67735
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please reserve this forum for questions an advanced Java. I have moved this most to a more appropriate forum.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 261
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic