is it possible to use an if statement using sleep
e.g if Thread.Sleep > 300000
if not how can i overcome this
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
is it possible to use an if statement using sleep
e.g if Thread.Sleep > 300000
if not how can i overcome this
No it is not possible to do that.
But I can see what your trying to do and you can get the desired result like this:
public class Dave { /** * JavaProgrammingForums.com */ public static void main(String[] args) throws Exception { int sleepTime = 3500; //Thread.sleep(sleepTime); if(sleepTime > 3000){ // Do something } } }
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.
I think he wants to know how long a certain thread has been sleeping for. You'll at least need 2 static/object variables I think. One to hold the Thread and another to hold the time sleep started. The Thread variable isn't entirely necessary, but is if you want to do something with that Thread (like wake it).
import java.util.Calendar; public class Dave implements Runnable { Thread running; long start; public static void main (String[] args) throws InterruptedException { new Dave().myInit(); } public void myInit () throws InterruptedException { // run a thread to be interrupted running = new Thread(this); running.start(); // wait for thread to go into timed waiting while (running.getState() != Thread.State.TIMED_WAITING) { } // a little delay try { Thread.sleep(1000); } catch (InterruptedException e) { } System.out.println("interrupting another thread"); running.interrupt(); // run a thread that will not be interrupted new Thread(this).start(); } public void run () { start = Calendar.getInstance().getTimeInMillis(); try { System.out.println("Sleeping"); Thread.sleep(3000); System.out.println("I was not interrupted"); System.out.println("I sleept for " + (Calendar.getInstance().getTimeInMillis() - start)); } catch (InterruptedException e) { System.out.println("I was interrupted!"); System.out.println("I sleept for " + (Calendar.getInstance().getTimeInMillis() - start)); } } }