Jon said:
I really don't think it's necessary though. Being static, if anyone
*is* confused, they'll *know* they're confused immediately, and can
consult the documentation to become unconfused. With your way of
writing code, people are likely to get confused and *stay* confused
about what Thread.sleep does.
I'll borrow your example from below..
Thread t = new Thread (....);
t.start();
t.sleep(500);
As long as this compiles, then the problem exists. There is nothing that
would stop anyone from writing it. The confusion is not the result from
seeing Thread.currentThread().sleep(..), but from having start,
interrupt, yield, sleep, ... as methods in the same class that can do
very unpredictable things..
Now, let's put the novice hats on and compare *readability*:
// A
Thread t = new Thread (....);
t.start();
Thread.currentThread().sleep(500);
// B
Thread t = new Thread (....);
t.start();
Thread.sleep(500);
Now, consider renaming t to thread..
// C (common? oh yes)
Thread thread = new Thread (....);
thread.start();
Thread.sleep(500);
Does A encourages t.sleep()? I don't see that. However, A is by far
the most readable.
It would give the right impression about what is happening, but the
wrong impression about what the Thread.sleep method itself does. Ask
yourself which convention is more likely to lead to the reader
eventually writing code such as:
Thread t = new Thread (....);
t.start();
t.sleep(500);
And that's the heart of the issue..
If it's a way to learn the API, you score all the points.
However, I believe strongly that this class in particular is critical
enough that I'd make sure that sleep in only called using a reference to
currentThread.
Actually, it's a common design for thread classes, I believe - static
methods apply to the current thread, because that's the only thread it
makes sense for them to apply to. Instance methods apply to the method
they're called on. That's applied consistently - contrary to your post
a while ago which claimed that isInterrupted gives information about
the current thread, it gives information about the thread instance it's
called on.
You're referencing the following:
and that was a bad comment, resulted from cut+paste from previous line.
It should've been: "/* instance method */". Debugging comments is far
more difficult than debugging code.
However, you did touch another call that you find debated in lunch rooms:
Thread.interrupted()
vs.
Thread.currentThread().isInterrupted()
I do prefer the latter (to the surprise of few), and hope this won't be
questioned by the static methods police..
I don't find it in the least bit confusing - but I'm sure I would if I
were a new programmer who encountered code which called the static
method via an instance reference.
Jon, maybe you don't find it confusing, but it is a nasty design. At
any given point, you could call start, stop, pause, interrupt, sleep,
yield, all as methods in the same class, all are legal when called using
an instance. That would certainly not be your design.