Results 1 to 13 of 13
Thread: How to use sleep() method
- 05-31-2007, 03:45 PM #1
Member
- Join Date
- May 2007
- Posts
- 11
- Rep Power
- 0
- 05-31-2007, 03:46 PM #2
Member
- Join Date
- May 2007
- Posts
- 10
- Rep Power
- 0
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 }
- 12-29-2008, 05:11 PM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 10
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.
Neil Coffey
Javamex - Java tutorials and performance info
- 01-21-2009, 06:38 AM #4
it can be invoked using thread name
for example thread1.sleep(1000);
this waits for 1 sec... 1000 milli-seconds
- 01-21-2009, 03:48 PM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 10
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.
Neil Coffey
Javamex - Java tutorials and performance info
-
Agrees with neil. It's not good coding form to call static methods on the instance as suggested by Thennarasu.
- 01-22-2009, 06:07 AM #7
Gaints, sorry for the post actually i didnt mean to cal using object...
One Life!!! Y Serious??? :)
- 02-03-2009, 11:15 AM #8
Member
- Join Date
- Feb 2009
- Posts
- 1
- Rep Power
- 0
hi
Hi All,
Thanks
- 02-15-2009, 09:19 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 10
- Rep Power
- 0
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
- 02-15-2009, 11:10 PM #10
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 10
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.Neil Coffey
Javamex - Java tutorials and performance info
- 02-17-2009, 09:50 PM #11
- 10-06-2009, 08:34 AM #12
Member
- Join Date
- Oct 2009
- Posts
- 1
- Rep Power
- 0
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......
-
Similar Threads
-
How to use sleep method of the Thread class
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:42 PM -
How to use sleep() to wait for a while
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:32 PM -
The Sleep Scripting Project 2.1-b21
By Java Tip in forum Java SoftwareReplies: 0Last Post: 03-29-2008, 01:00 PM -
Can't get my thread to sleep!
By jamesfrize in forum New To JavaReplies: 2Last Post: 03-25-2008, 05:14 AM -
How to use the sleep and thread?
By jiuhu in forum Java AppletsReplies: 4Last Post: 08-07-2007, 02:56 AM
Bookmarks