简体   繁体   中英

Java example: Is this really accessing a static method in a non-static way?

I am just working through some examples from my text book, writing the code in Eclipse to figure it out.

This is the code for the method I have a question about:

public void run() {
    //Get the lock before entering the loop
    synchronized(getClass()) {
        for (int i =0; i<N; i++) {
            System.out.println(getName() + " is tired");
            
            try{
                Thread.currentThread().sleep(DELAY);
            } catch (InterruptedException e) {}
            
            System.out.println(getName() + " is rested");
        }

On line the line with:

Thread.currentThread().sleep(DELAY)

Eclipse gives a warning: "The static method sleep(long) from the type Thread should be accessed in a static way". Eclipse suggests the following solution:

Thread.currentThread();
Thread.sleep(DELAY);

I don't understand why it makes a difference (if it really does). Can someone please explain?

sleep() is a static method and causes the current thread to sleep for the specified amount of time. Thus the currentThread() call is superfluous, and can potentially be confusing since it almost implies that you could make another thread sleep by using similar code (you can't).

The best way to write that code is simply:

Thread.sleep(DELAY);

Code in question has nothing to do with threads. Eclipse just says that you should access static method sleep directly via class name, not via instanse: Thread.sleep(DELAY) .

Any static method static ... f() of class C should be accessed as Cf() for clarity (and brevity).

The Thread.sleep() is already a static method and affects the current thread. There is no need to call currentThread().

Thread.delay is a static method. It doesn't require an instance to invoke it - you can just say Thread.delay().

Thread.currentThread() returns you an reference to the current thread. You can invoke a static method on it, but you don't strictly need the reference and it's not used at all. You could equally do this:

new Thread().delay(100);

... and it would still make the current thread sleep - not the thread that we just created.

So eclipse is telling you that the better, less confusing way is Thread.delay(100).

Correct me if I'm wrong but the warning probably is "The static method ... should be accessed in a static way " (not ... should be accessed in a non-static way ).

In fact it is confusing to call

someThread.sleep(1000)

because as Thread.sleep() is a static method in is not called "against" someThread but on the current Thread.

So the message says "access static methods in a static way" :)

It does not make any difference in execution. However second option Thread.sleep() is more readable as user does not get confused thinking its an instance method.

通过将方法作为Thread.currentThread().sleep()它给人的印象是它是一个实例方法,为了避免混淆,Eclipse 建议您通过所有者类访问静态方法,从而表明它们实际上是, 静止的。

As Victor Sorokin wrote the warning has nothing to do with the example code with Threads. Here is another example where, in Eclipse, we get the same compiler warning:

OptionBuilder.withArgName(PROPERTY_ARG).hasArg(true).isRequired(false).create(PROPERTY_ARG_OPTION);

This is using the Apache Commons CLI library. OptionBuilder uses the 'fluent' builder pattern. The only way I see to fix the warning is use the recommended Eclipse hint, that breaks the line into multiple statements, or prepend an annotation to ignore the warning.

Thread currentThread = Thread.currentThread();
currentThread.wait(DELAY); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM