What is the difference between Thread.sleep(10) and Thread.currentThread().sleep(10) ?

G

Gonzalo Moreno

Hi,

I'm trying to understand the conceptual difference between

Thread.currentThread().sleep(10) and
Thread.sleep(10)

Is there any difference between the two? It seems to me that you could
simplify Thread.currentThread().sleep(10) to Thread.sleep(10), since
according to the Java API, currentThread "returns a reference to the
currently executing thread object," and the sleep method causes the
"currently" executing thread to sleep ... what am I missing here??

thanks in advance,
Gonzalo.
 
M

Matt Humphrey

Gonzalo Moreno said:
Hi,

I'm trying to understand the conceptual difference between

Thread.currentThread().sleep(10) and
Thread.sleep(10)

Is there any difference between the two? It seems to me that you could
simplify Thread.currentThread().sleep(10) to Thread.sleep(10), since
according to the Java API, currentThread "returns a reference to the
currently executing thread object," and the sleep method causes the
"currently" executing thread to sleep ... what am I missing here??

You're not missing anything. Java (unfortunately) permits static methods to
be accessed through instance objects. The real problem in your example
would occur if you were to say someOtherThread.sleep (10) thinking it
would make the other thread sleep. Although the syntax is legal, it still
makes only the current thread sleep. In general, you're better off
referring to static methods directly.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
J

jonathanmcdougall

I'm trying to understand the conceptual difference between
Thread.currentThread().sleep(10) and
Thread.sleep(10)

Is there any difference between the two?
No.

what am I missing here??

Thread.currentThread() returns a reference to the current thread so
you might have a reference pointing to that. It can be useful to
have a reference to the current thread. But if you know for certain
that you want to call a function on the current thread, don't bother.


Jonathan
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top