• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in synchronized thread program

 
Saumya Srivastava
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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).
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
@pete

thank you sir
keep guiding us
have a nice time
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic