Forums Register Login

Pausing a Thread

+Pie Number of slices to send: Send
Hi!

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

http://download.oracle.com/javase/tutorial/essential/concurrency/sleep.html

it says the sleep time is not guaranteed.

I have found this from the web to really sleep for the time specified:

From: http://www.markstechstuff.com/2006/09/java-pause-method.htm


But problem with this code is that it is doing looping and it blocks the other threads from executing.

Anyone has any ideas to really pause the thread for exactly the time specified?

Thanks
+Pie Number of slices to send: Send
I think Thread.sleep(5000) should do the trick.

The site you provided states:

these sleep times are not guaranteed to be precise


But I don't think your reading from telnet needs to be that precise.
+Pie Number of slices to send: Send
What you mean by 'The method is multi-threaded' ? You mean it is concurrently accessed by more than one thread?
1
+Pie Number of slices to send: Send
 

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?
+Pie Number of slices to send: Send
Hi!

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.

+Pie Number of slices to send: Send
Another method is not to wait for 5 seconds, but to read in a String pattern 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?

Thanks.
+Pie Number of slices to send: Send
An infinite loop on its own won't overflow the stack. There must be another explanation.
If you include the text "RESPOND OK" in the real message, it will terminate prematurely.
+Pie Number of slices to send: Send
 

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).

Bill


CLUCK LIKE A CHICKEN! Now look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1507 times.
Similar Threads
testing concurrency and sleeping threads
Threads & Locks??
Is System.currentTimeMillis() call costly
Synchronizing between two threads, one implements Runnable the other extends Thread
JPA(Toplink Essentials) using JTA on Glassfish: Issue In Loading References
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 02:11:15.