Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Java Timeout

  Asked By: Cesar    Date: Jan 12    Category: Java    Views: 686
  

does anyone know how do I crate a sleep method in Java. I have one
function and I want to wait 30 second first and then I want that
function to execute. If anyone know then please let me know. I
really appriciate your help.

Share: 

 

5 Answers Found

 
Answer #1    Answered By: Sultana Tabassum     Answered On: Jan 12

This is a little class I have used and it worked just
fine, doing exactly what you want:

class ALittleWait extends Thread {
private int sleepTime;
// Just using the Thread class
// by calling superclass Thread constructor
// to get a sleeping effect for a little while
public ALittleWait(String name)
{
super(name);
sleepTime = (int) (5000);
}
}

And you call it from your main class (or whatever)
with:

ALittleWait.sleep(2000);

 
Answer #2    Answered By: Hollie Hughes     Answered On: Jan 12

use the sleep  method in Thread class
refer to java  api for details

 
Answer #3    Answered By: Jackson Williams     Answered On: Jan 12

I really appriciate your help. But, In my Java program I hava a
applet and I am not using thread at all. So, my question is this
that can I still do I really have to use Thread there no other way to
wait in program without using Thread. Please let me know if you know
the answer.

 
Answer #4    Answered By: Ethan Evans     Answered On: Jan 12

If you're just using the sleep  method in Thread class, you don't have
to extend the Thread class especially you are already extending the
Applet class. Try this sample:

public class SampleSleep {
//Put the throws Exception class to the calling method
public static void main(String[] args) throws Exception {
System.out.println("Hello World will print after 5 seconds...");
//call the sleep method  of the Thread class
Thread.sleep(5000);
System.out.println("Hello World");
}
}

Just don't forget to put the throws Exception to the calling method.

 
Answer #5    Answered By: Komal Mohammad     Answered On: Jan 12

Try the following

MS_TO_SLEEP = 10 * 1000; // sleep  for 10 seconds
try
{
Thread.sleep(MS_TO_SLEEP);
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}

 
Didn't find what you were looking for? Find more on Java Timeout Or get search suggestion and latest updates.




Tagged: