开发者

Java - Accessing Static Method Sleep - What's wrong?

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 开发者_StackOverflowwhich I want to sleep for a while.


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.


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.


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

Thread.currentThread().sleep(2000);

is the same as

Thread.sleep(2000);


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.


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().


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜