How to pause a running program

Discussion in 'Java' started by Rick, Oct 7, 2003.

  1. Rick

    Rick Guest

    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
    Rick, Oct 7, 2003
    #1
    1. Advertising

  2. On Wed, 08 Oct 2003 06:13:49 +1000, Rick wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Oct 7, 2003
    #2
    1. Advertising

  3. Rick

    Brad BARCLAY Guest

    Rick wrote:
    > 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

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org
    
    Brad BARCLAY, Oct 7, 2003
    #3
  4. On Tue, 07 Oct 2003 20:41:36 GMT, Brad BARCLAY wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Oct 7, 2003
    #4
  5. Rick

    Roedy Green Guest

    On Wed, 08 Oct 2003 06:13:49 +1000, Rick <rrquick@nospam-com> wrote or
    quoted :

    >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

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Oct 8, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amit Nath

    Pause a program while running

    Amit Nath, Jun 29, 2004, in forum: C Programming
    Replies:
    10
    Views:
    606
    Wynand Winterbach
    Jul 21, 2004
  2. Basilisk96
    Replies:
    3
    Views:
    516
    Steven D'Aprano
    May 13, 2007
  3. Making a program pause

    , Dec 24, 2007, in forum: C Programming
    Replies:
    21
    Views:
    1,244
    Kelsey Bjarnason
    Jan 2, 2008
  4. still me

    Pause Perl Program?

    still me, Jul 8, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    160
    Lambik
    Jul 8, 2007
  5. Tuxedo
    Replies:
    7
    Views:
    421
    J. Gleixner
    Aug 16, 2012
Loading...

Share This Page