Results 1 to 13 of 13
  1. #1
    Hasan is offline Member
    Join Date
    May 2007
    Posts
    11
    Rep Power
    0

    Default How to use sleep() method

    I want to stop my program during 1 second, how can I do that and which package should I use?

  2. #2
    Valeriano is offline Member
    Join Date
    May 2007
    Posts
    10
    Rep Power
    0

    Default

    It is already available in java.lang package. Try following code:

    Java Code:
    try{
      //do what you want to do before sleeping
      Thread.currentThread().sleep(1000);//sleep for 1000 ms
      //do what you want to do after sleeptig
    }
    catch(ItrerruptedException ie){
    //If this thread was intrrupted by nother thread 
    }

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    In case anyone is interested in more information on using Thread.sleep() and how its behaviour is affected by system performance, I have some information on my web site on Thread.sleep() behaviour. One important point to note is that if timing is fairly critical to your application (note that you can never make it very critical with Thread.sleep()), e.g. for animation, then you should generally time how long the thread actually slept and make appropriate adjustments.

  4. #4
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    it can be invoked using thread name
    for example thread1.sleep(1000);
    this waits for 1 sec... 1000 milli-seconds

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Well, any static method in the language can be invoked on an object reference instead of the class. But that doesn't change the functionality: Thread.sleep() is a static method that puts the current thread to sleep.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Agrees with neil. It's not good coding form to call static methods on the instance as suggested by Thennarasu.

  7. #7
    Thennarasu's Avatar
    Thennarasu is offline Member
    Join Date
    Jan 2009
    Location
    Chennai, Tamilnadu
    Posts
    39
    Rep Power
    0

    Default

    Gaints, sorry for the post actually i didnt mean to cal using object...
    One Life!!! Y Serious??? :)

  8. #8
    santoshg_85 is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default hi

    Hi All,


    Thanks

  9. #9
    jason_check is offline Member
    Join Date
    Feb 2009
    Posts
    10
    Rep Power
    0

    Default

    I suggest you keep checking the total sleep time if the sleep time is critical in your code. For example every 100 msec.
    View, validate and edit X9.37, X9.100-180 and UCD ICL files.
    http://www.digertech.com

  10. #10
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    Agree that if it matters to you how long the thread actually sleeps, then you need to measure this, because it's not necessarily (and usually won't be) exactly the requested sleep time (see, for example, the measurements of Thread.sleep() behaviour that I've made under XP with different levels of system load).

    However, I'm not sure about your point with the 100ms.

  11. #11
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    6

    Default

    Quote Originally Posted by jason_check View Post
    I suggest you keep checking the total sleep time if the sleep time is critical in your code. For example every 100 msec.
    I suggest that you redesign your code so that the sleep time is not critical. At best, the sleep time is an approximation. If its critical, do something else.

  12. #12
    ganeshpadole is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Thumbs up Problem solving

    use try catch block like as follow

    try
    {

    //here will be your code to execute before sleeping

    Thread.sleep(1000); // Thread is a class which has a static method sleep(time in milliseconds), so we can access static method with the help of it's class name & dot(.) operator.

    //hence system will go on sleep

    then do code here after sleep like exit you program,

    use

    System.exit(0);

    }

    //Exception handling

    catch(Exception e)
    {
    System.out.println(e);
    }


    Thats all dear......

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. How to use sleep method of the Thread class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:42 PM
  2. How to use sleep() to wait for a while
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:32 PM
  3. The Sleep Scripting Project 2.1-b21
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 03-29-2008, 01:00 PM
  4. Can't get my thread to sleep!
    By jamesfrize in forum New To Java
    Replies: 2
    Last Post: 03-25-2008, 05:14 AM
  5. How to use the sleep and thread?
    By jiuhu in forum Java Applets
    Replies: 4
    Last Post: 08-07-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •