views:

202

answers:

6

When I put the code below in NetBeans, NetBeans gives me a warning next to it saying "Accessing static method sleep".

        try {
            Thread.currentThread().sleep(2000);
        }
        catch(InterruptedException ie){
            //continue
        }

Am I doing something wrong? Should I be calling this differently? I'm not doing anything multi-threaded. I just have a simple main method that which I want to sleep for a while.

+4  A: 

sleep is static, so you access it with Thread.sleep(2000);. It affects the current thread.

From the javadoc:

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. The thread does not lose ownership of any monitors.

What that means is that you can't sleep another thread, only the one that the code is in.

Paul Tomblin
Someone want to explain the downvote?
Paul Tomblin
+2  A: 

Thats because the sleep() method is declared as static therefore

Thread.currentThread().sleep(2000);

is the same as

Thread.sleep(2000);
stacker
+9  A: 

Thread.currentThread() returns an instance of the Thread class. When invoking a static method you only want to work on the class itself. So invoking a static method on current thread you will get a warning you are calling the method on an instance.

You would just call Thread.sleep(2000); It would be equivalent to Thread.currentThread.sleep(2000);

This is important to know because people have been burned doing something like:

Thread a = new Thread(someRunnable);

a.start();
a.sleep(2000);  //this will sleep the current thread NOT a.

Edit: So how do we sleep a? You sleep a by writing the sleep invocation within the runnable passed in to the constructor, like:

Runnable someRunnable = new Runnable(){
    public void run(){
        Thread.sleep(2000);
    }
};

When 'a' is started, Thread.currentThread in someRunnable's run method is the 'a' thread instance.

John V.
So how do you sleep a?
Greg
@Greg, you don't 'sleep a', you send a message to 'a' that it needs to go to sleep. One way to do that is to have `run` look for a variable being set, and if the variable is set it goes to sleep, and a method in 'a' that an outside caller can use to set that variable.
Paul Tomblin
+1  A: 

netbeans giving you warning because you are accessing static method from Thread reference not from Thread class. try this

try {
            Thread.sleep(2000);
        }
        catch(InterruptedException ie){
            //continue
        }

sleep method Causes the currently executing thread to sleep.So no need to call Thread.currentThread().

Vivart
+1  A: 

Whenever you try to access static method using object it is not best practise, NB gives warning at that time, here is the same case

Thread.currentThread() will return you a object of Thread

org.life.java
+2  A: 

There is no method on Thread instances that corresponds to "sleep(long)".

Thread.currentThread().sleep(2000); does compile, however, because there is a method on the thread class that is called sleep() with a long argument.

Java allows this as a compiler time trick so that new coders could execute this when they are confused about the static access of methods.

However, what this actually is resolved to in the compiler is:

Thread.sleep(2000);

The following code is also equivalent:

Thread t = new Thread(new Runnable() { public void run() { // do nothing } }); t.sleep(2000);

As one poster (John V) has pointed out, this doesn't make the actual thread (t) instance sleep - the current thread that created the thread object is put to sleep.

The warning exists such that you remember that you're accessing a static method of a class, not a method of an instance variable.

The appropriate code to write is always Thread.sleep(2000); - never access static methods through an instance to avoid confusion and this warning.

MetroidFan2002