Solved

Sleep() ...

Posted on 2000-03-03
7
289 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
Comment Utility
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
Comment Utility
0
 

Expert Comment

by:progra
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:izi13
Comment Utility
Thanx
0
 

Expert Comment

by:arulthas
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now