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
posted
0
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.
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
posted
0
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.
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
posted
0
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
posted
0
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
posted
0
[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."