• 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

Doubt in synchronized thread program

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My doubt it that how the program below is running. I thought it should have a compilation error. As sleep() function is in Thread class. And in the following code, the class is not extending the Thread class only implementing the Runnable. Can you please explain it why?
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saumya Srivastava wrote:
My doubt it that how the program below is running. I thought it should have a compilation error. As sleep() function is in Thread class. And in the following code, the class is not extending the Thread class only implementing the Runnable. Can you please explain it why?



Thread.sleep(...) is a static method and can be called anywhere (as far as I know).
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
even if we are not extending the Thread class
we are calling that methods using the class name that is in the java.lang package
and we know java.lang package is imported by default

now we are calling the sleep method using the class name
i.e.

Thread.sleep();

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasad Kharkar wrote:
this can be considered similar to Wrapper class
we can cal parseInt(String) method without extending the Integer class as follows

Integer.parseInt("123");



I respectfully beg to differ as this has nothing to do with wrapper classes, and one can easily create wrapper classes that have no static methods at all. This has everything to do with calling a static method on a class, nothing more and nothing less.

Cheer
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@pete

thank you sir
keep guiding us
have a nice time
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic