Solved

Sleep() ...

Posted on 2000-03-03
7
295 Views
Last Modified: 2012-05-04
Hi,
I would like my application to wait 5 seconds in a specific situation. In the documentation I read it can be done by Sleep() method. This is a method of Thread class.
My application has only one - the main - thread. How can I get its instance ?
Is there something like getMainThread() available ?

Thanx.
0
Comment
Question by:izi13
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 2580441
sleep is a static method. It's signature is:

public static void sleep(long millis);

So you can sleep for 5 seconds by writing:

Thread.sleep(5000);

A static method exists apart from any instance of a class, and is accessible by prepending the class name as shown.
0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 2580444
0
 

Expert Comment

by:progra
ID: 2585038
Hi
You can use this method

static Thread currentThread()
          Returns a reference to the currently executing thread object.

and then make sleep()
                currentThread() .sleep()
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:izi13
ID: 2587102
Thanx
0
 

Expert Comment

by:arulthas
ID: 3703416
hi imladris,

i tried your method but it give and error. that error is as below :-

HelloWorld.java:4: 'class' or 'interface' expected
public static void sleep(long millis);
              ^
1 error


0
 

Expert Comment

by:arulthas
ID: 3703572
hi imladris,

i tried your method but it give and error. that error is as below :-

HelloWorld.java:4: 'class' or 'interface' expected
public static void sleep(long millis);
              ^
1 error


0
 

Expert Comment

by:progra
ID: 3705883
hi arulthas,

if you want pause current tread:
//Time to sleep
int timeToSleep;
//Current thread object
Thread thr = Thread.currentThread();
//Pause thread
thr.sleep(timeToSleep);

or shorter
Thread.currentThread().sleep(timeToSleep);
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question