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.

Results 1 to 3 of 3

Thread: If statement

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default If statement

    is it possible to use an if statement using sleep

    e.g if Thread.Sleep > 300000

    if not how can i overcome this


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: If statement

    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.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: If statement

    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));
            }
        }
    }

Similar Threads

  1. Error of "Class has no return statement"
    By mdstrauss in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 2nd, 2009, 01:00 PM
  2. [SOLVED] Switch statement question
    By shikh_albelad in forum Loops & Control Statements
    Replies: 5
    Last Post: May 31st, 2009, 06:13 AM