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

Discussion in 'Java' started by Gonzalo Moreno, Apr 5, 2004.

  1. 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.
     
    Gonzalo Moreno, Apr 5, 2004
    #1
    1. Advertising

  2. "Gonzalo Moreno" <> wrote in message
    news:...
    > 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 http://www.iviz.com/
     
    Matt Humphrey, Apr 5, 2004
    #2
    1. Advertising

  3. Gonzalo Moreno

    Guest

    > 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
     
    , Apr 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clive
    Replies:
    1
    Views:
    647
    Sherif ElMetainy
    Aug 28, 2003
  2. jakk
    Replies:
    4
    Views:
    12,214
  3. Zalek Bloom
    Replies:
    14
    Views:
    679
    Michael Borgwardt
    Nov 21, 2003
  4. JohnnyV

    Thread.CurrentThread.CurrentCulture

    JohnnyV, Feb 9, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    197
    JohnnyV
    Feb 9, 2004
  5. Iñaki Baz Castillo

    Difference between "sleep()" and "Thread.stop"?

    Iñaki Baz Castillo, Sep 10, 2008, in forum: Ruby
    Replies:
    2
    Views:
    196
    Robert Klemme
    Sep 10, 2008
Loading...

Share This Page