aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in synchronized thread program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in synchronized thread program" Watch "Doubt in synchronized thread program" New topic
Author

Doubt in synchronized thread program

Saumya Srivastava
Ranch Hand

Joined: Mar 19, 2007
Posts: 45
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?


OCPJP 6.0
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Mar 07, 2010
Posts: 446
    
    1

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();


SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Mar 07, 2010
Posts: 446
    
    1

@pete

thank you sir
keep guiding us
have a nice time
 
 
subject: Doubt in synchronized thread program