How to pause a running program

R

Rick

Hi,

I need to pause inside my program for 5 seconds. The main class does not
extend Thread and does not implement runnable so I can't use the "sleep"
method on it. Is there someway I could pause the program and acheive the
same effect sleep() would on a thread? Can someone point me out how to
do this? Thanks

Rick
 
G

Gordon Beaton

I need to pause inside my program for 5 seconds. The main class does
not extend Thread and does not implement runnable so I can't use the
"sleep" method on it. Is there someway I could pause the program and
acheive the same effect sleep() would on a thread? Can someone point
me out how to do this? Thanks

So you didn't even *try* using Thread.sleep()?

Every program consists of at least the main thread, regardless of
whether you or the JVM have created any additional ones.

Go ahead and try, you won't break anything.

/gordon
 
B

Brad BARCLAY

Rick said:
I need to pause inside my program for 5 seconds. The main class does not
extend Thread and does not implement runnable so I can't use the "sleep"
method on it. Is there someway I could pause the program and acheive the
same effect sleep() would on a thread? Can someone point me out how to
do this? Thanks

Try calling "wait(5000L)". "wait(long)" is a method in
java.lang.Object, so all objects can call it (although you should be
synchronized on the current object before calling it). HTH!

Brad BARCLAY
 
G

Gordon Beaton

Try calling "wait(5000L)". "wait(long)" is a method in
java.lang.Object, so all objects can call it (although you should be
synchronized on the current object before calling it). HTH!

While wait() can be used to pause a running thread as you've
suggested, that certainly isn't what it's intended for. The same could
be said about calling SocketInputStream.read() after setting
SO_TIMEOUT.

The fact that wait() can only be invoked from within a synchronized
block adds unnecessary complication to an otherwise simple operation
(sleeping), and makes it less obvious what to do in a static method,
especially for a beginner who doesn't realize that even the main
thread can call a method such as Thread.sleep().

Also, it has the potentially undesirable side effect of preventing
other threads from entering the block, at least until wait() itself is
entered and the monitor dropped.

Thread.sleep() is the way to go, despite his initial worries.

/gordon
 
R

Roedy Green

I need to pause inside my program for 5 seconds. The main class does not
extend Thread and does not implement runnable so I can't use the "sleep"
method on it. Is there someway I could pause the program and acheive the
same effect sleep() would on a thread? Can someone point me out how to
do this? Thanks

It does not have to. Further, it is trivial matter to make it do so.
Just add a run method and add "implements Runnable".

See http://mindprod.com/jgloss/sleep.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top