Reputation: 484
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?
Upvotes: 0
Views: 2023
Reputation: 1
Thread currentThread = Thread.currentThread();
currentThread.wait(DELAY);
Upvotes: -1
Reputation: 952
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.
Upvotes: 0
Reputation: 12770
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" :)
Upvotes: 1
Reputation: 500227
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);
Upvotes: 9
Reputation: 16545
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).
Upvotes: 1
Reputation: 78579
By accesing the method as Thread.currentThread().sleep()
it gives the impression that it is an instance method, to avoid confusion, Eclipse suggests you access static methods through the owner class and by this make evident that they are, in fact, static.
Upvotes: 0
Reputation: 11996
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 C.f()
for clarity (and brevity).
Upvotes: 3
Reputation: 4413
The Thread.sleep() is already a static method and affects the current thread. There is no need to call currentThread().
Upvotes: 2
Reputation: 9307
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.
Upvotes: 0