Two Laptop Bag*
The moose likes Java in General and the fly likes Pausing a Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pausing a Thread" Watch "Pausing a Thread" New topic
Author

Pausing a Thread

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 132
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
Peter van de Riet
Ranch Hand

Joined: Apr 09, 2004
Posts: 112

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.


Each number system has exactly 10 different digits.
Maroun Baydoun
Greenhorn

Joined: Sep 23, 2011
Posts: 4

What you mean by 'The method is multi-threaded' ? You mean it is concurrently accessed by more than one thread?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

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?
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 132
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.

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 132
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
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


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pausing a Thread