Solved

Sleep() ...

Posted on 2000-03-03
7
290 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 69
topping2 challenge 13 80
Securing Jmx Console and web console 2 64
Spring Framework HTTPSession management 1 23
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 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

26 Experts available now in Live!

Get 1:1 Help Now