Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sleep() ...

Posted on 2000-03-03
7
Medium Priority
?
299 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 200 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

722 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