Re: this.wait( sleep );

From: Jon Skeet (skeet_at_pobox.com)
Date: 02/26/04


Date: Thu, 26 Feb 2004 16:12:26 -0000

Ricardo <spam@spam.spam> wrote:
> > 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..

They don't do unpredictable things at all. The confusion is the result
of being able to call static methods as if they were instance methods,
which then leads people to think that they operate on the instance
they're talking about.

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

Novice programmer reads your code. He hasn't encountered Thread.sleep
before. He reads the code and thinks, "Ah, Thread.sleep must be an
instance method which sends whatever thread it's called on to sleep."

Later, when he's writing some other code where he (foolishly) wants to
make another thread pause, he calls t.sleep() and then gets confused
when it makes "the wrong thread" sleep.

> However, A is by far the most readable.

In your view - not in mine. It gives a false impression of the nature
of the Thread.sleep call, in my view - and that doesn't add to
readability, it reduces it.

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

If the programmer knows the API already, then it's irrelevant, as
they'll know that Thread.sleep makes the current thread sleep.

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

*You* might do that - but will everyone else who's read your code and
got the wrong end of the stick?

> > 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:
>
> >>> ....
> >>> resume() /* DEPREATED, instance method */
> >>> interrupt() /* instance method */
> >>> interrupted() /* static method, Current Thread */
> >>> isInterrupted() /* instance method, Current Thread */
>
> 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.

Right. However, it does mean that the Thread class is entirely
consistent.

> 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 particularly mind either of them. What I *would* object to is

Thread.currentThread().interrupted()

as again, that gives a false impression.

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

My design would be for the language to forbid calling
Thread.currentThread().sleep() in the first place (as C# does, btw).
>From that starting point, I see nothing particularly wrong in the
design of the Thread class. The "all static methods operate on the
current thread; all instance methods operate on the instance thread"
rule is hardly difficult to learn.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: CInternetSession
    ... the presence of the Sleepindicates the serious design flaw. ... correct model would be to simply use an OnTimer event that would trigger the next ... It never ceases to amaze me how programmers try to cram everything into the single-thread ... Sleep() calls around like pixie dust, your design is fundamentally broken and will need to ...
    (microsoft.public.vc.mfc)
  • Re: Zephram can okay borrow their slave
    ... out rob. ... It might jump ill resorts, do you design them? ... sleep them, then we very enforce Bernadette and Vance's important ...
    (sci.crypt)
  • Re: AfxBeginThread startup times and overhead
    ... the correct solution would be ... failures under load, lost packets, hangs, etc. stemmed from bad design like this. ... I took one look at the code, saw a Sleep() inside it, and said "This code will lose ... This is in direct contrast to waiting on a mutex, event, ...
    (microsoft.public.vc.mfc)
  • Re: CInternetSession
    ... polls a Web site every 10 seconds or so. ... The presence of the Sleep, however, would represent a bad design if the purpose is to ... If the Sleep() is gratuitous, the design is even worse, because it is still means the loop ... It never ceases to amaze me how programmers try to cram everything into the single-thread ...
    (microsoft.public.vc.mfc)
  • Re: GMT Sunday 1st July
    ... birds are singing ... Now, should I stay here or try for another hour's sleep - decisions, ... You can call me ItsMe if you want, but it could cause confusion with other posters. ... Joe is my real name, but I know it's also a common name so that could cause confusion too. ...
    (uk.people.silversurfers)