This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I want to pause the execution of the program for 5 seconds. The pause is actually required because I am reading from a device from telnet and I need to wait 5 seconds before the response comes in and I send another command.
My method is multi-threaded so Thread.sleep(5000) does not work because according to
Alan Blass wrote:Anyone has any ideas to really pause the thread for exactly the time specified?
That doesn't mean anything useful. What does "exactly" mean? Would you reject a method which turned out to pause for 5 seconds plus 1 nanosecond?
I'm suspecting you wouldn't (and anyway I don't think anybody could produce code in any language on any operating system which could pause a thread for an interval with precision of less than a nanosecond.) So what are your actual precision requirements for this pause?
Joined: Mar 21, 2010
Thanks for your replies.
Actually I wanted it to pause for at least 5 seconds. So that I get to wait for the device I am telnet into to respond to my commands. Otherwise the next command I sent will not be received properly by the device.
I am sending it concurrently to 12 devices. Some of the device is giving me an error code.
I am suspecting that the pause is not at least 5 seconds. That's why I am getting an error for some of the devices and not the others.
Please help. Thanks.
Joined: Mar 21, 2010
Another method is not to wait for 5 seconds, but to read in a Stringpattern such as "RESPOND OK" provided by the device. My code to read:
But I sometimes get a stack overflow from Apache Tomcat. Is suspect it is the infinite while loop that is giving Apache a stack overflow. Can someone show me a better read method?
Alan Blass wrote:
I am suspecting that the pause is not at least 5 seconds.
It is my understanding that you are guaranteed at least the sleep length specified - there is no guarantee that it will wake up exactly at that time - may be later but never earlier (unless interrupted).