P
P.Hill
Apparently this is NOT how I tell the VM that I
won't to sleep. If is not how do I?
-Paul
won't to sleep. If is not how do I?
-Paul
P.Hill said:Apparently this is NOT how I tell the VM that I
won't to sleep. If is not how do I?
-Paul
P.Hill said:Apparently this is NOT how I tell the VM that I
won't to sleep. If is not how do I?
-Paul
Thread.currentThread().sleep();
Thread.currentThread().sleep(time-in-milliseconds)
Jezuch said:U?ytkownik Ricardo napisa?:
WARNING!
This is bogus.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#sleep(long)
Thread.sleep() is *static*
Jezuch said:U?ytkownik Ricardo napisa?:
WARNING!
This is bogus.
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html#sleep(long)
Thread.sleep() is *static*
Matt said:Wrong, it's not bogus. It is exactly how you make the currently executing
thread go to sleep...
Tony said:As already mentioned in this thread (no pun intended), this is NOT
recommended.
In fact, referencing any static member non-statically is very poor form.
The language designers overlooked this behaviour, and if we could turn
back time, the compiler wouldn't allow it.
Thread.sleep(long) is the only recommended way to achieve what you are
trying.
Carl said:Perhaps bogus is too strong a word. It is, however, of such bad style
that it should never be done that way.
You should never call a static method on an instance. It just makes it
harder to see what's going on. People (like you seem to) might even
think that using the instance is necessary, which can lead to really bad
coding practices.
Just call write:
Thread.sleep(500);
if you want a half-second pause.. Getting an instance just makes things
more confusing and (in a very minor way) less efficient.
Carl said:Perhaps bogus is too strong a word. It is, however, of such bad style
that it should never be done that way.
You should never call a static method on an instance. It just makes it
harder to see what's going on. People (like you seem to) might even
think that using the instance is necessary, which can lead to really bad
coding practices.
Just call write:
Thread.sleep(500);
if you want a half-second pause.. Getting an instance just makes things
more confusing and (in a very minor way) less efficient.
Matt Parker said:By stating Thread.sleep(time-in-milliseconds) I implied the use of
Thread.sleep(long). I certainly wouldn't use this in anything other than
something trivial like animation in an applet though. But the OP didn't
state what they wanted, they merely asked how to make a Thread object
sleep, and my answer was correct in this regard.
In a real (read non-trivial) application I would synchronize against a
locking object/method until the state that was required was able to
.notify() the lock, rather than rely on the vaguaries of timing.
Matt
Thread.currentThread().sleep(time-in-milliseconds)
if you want a half-second pause.. .
Tony said:Calling sleep using a Thread reference is bad practice.
practice.more that calling a static method on an instantiated class is bad
Yes, it does make a difference, a significant difference in fact.I don't see why you can dogmatically state that, since in reality it makes
no difference to code execution.
As already mentioned, the language designers allowed this one to slipAs a rule of thumb, I'd suggest that doing
whatever made the code more readable would be prefereable.
P.Hill said:Apparently this is NOT how I tell the VM that I
[want] to sleep. If is not how do I?
I'd suggest that doing whatever made the code more readable
would be prefereable.
In a real (read non-trivial) application I would synchronize
against a locking object/method until the state that was required
was able to .notify() the lock, rather than rely on the vaguaries
of timing.
Tony Morris said:As already mentioned, the language designers allowed this one to slip
through and it must stay in order to maintain reverse compability. There is
never a good reason to access a static member/method/class using a
non-static context.
As I was new to threads about a year ago, I found the non-static
variant to be much easier to understand. The static variant could
(for all I knew) have meant "put the JVM to sleep".
Oscar said:As I was new to threads about a year ago, I found the non-static variant
to be much easier to understand. The static variant could (for all I knew)
have meant "put the JVM to sleep". Since then I've certified myself and
know better, but I think one should never underestimate the cluelessness
of a newbie.
Of course, it would have been better to use something like this:
Thread.sleep(500); // Put the current thread to sleep for at least 500ms.
P.Hill said:P.Hill wrote:
[snip]
Wow, I ask a quick question, because I was lead down
the garden path by .wait() and had forgotten about
Thread.sleep( n ) and I generate a heated debate!
[snip]
But don't let me stop a good raging debate; have fun!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.