• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread sleep

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, What is the difference between the following two ways of calling sleep() method ?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Both are the same. as sleep() is a static method- one can call it using the Thread class or using an Instance of Thread class.

Thread.currentThread() - Returns a reference to currently executing thread object.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use Thread.sleep(). Static methods should always be called with a class identifier, or rather, they should never be called using an object reference. The latter is confusing.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan, I agree with you on "Static methods should always be called with a class identifier". Could you please advice the possible reason(s) "why static methods should not be called using reference ?"

Stephan van Hulst wrote:Use Thread.sleep(). Static methods should always be called with a class identifier, or rather, they should never be called using an object reference. The latter is confusing.

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because it makes the method appear part of the object, which it isn't. It's part of the class, and should be addressed that way.

The reason to do this is clarity. I can't think of a reason in favour of qualifying the method call with an object reference.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Hi Stephan, I agree with you on "Static methods should always be called with a class identifier". Could you please advice the possible reason(s) "why static methods should not be called using reference ?"

Stephan van Hulst wrote:Use Thread.sleep(). Static methods should always be called with a class identifier, or rather, they should never be called using an object reference. The latter is confusing.



Because, a static method can hide another static method. Calling it on the object(if it polymorphically referred) can cause problems. So, it's better to avoid it.
reply
    Bookmark Topic Watch Topic
  • New Topic