Solved

Sleep() ...

Posted on 2000-03-03
7
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java: The Public Class Main 4 66
how to debug htl and js pages 8 79
How to find out if all executorservice threads have finished 2 12
JVM error from eclipse 1 26
In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This video teaches viewers about errors in exception handling.
Suggested Courses

734 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