• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pausing a Thread

 
Alan Blass
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
Android Chrome Eclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maroun Baydoun
Greenhorn
Posts: 4
Java jQuery Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you mean by 'The method is multi-threaded' ? You mean it is concurrently accessed by more than one thread?
 
Paul Clapham
Sheriff
Pie
Posts: 20209
26
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47313
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13049
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic