File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread problem 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 "Thread problem" Watch "Thread problem" New topic
Author

Thread problem

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
class A implements Runnable{
public void run(){
try{
sleep(5000); //Compiler Error: Cannot find method
}catch(Exception e){}
System.out.println("RUN");
}
}
Please explain why this shows such an error?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

sharma ishu wrote:
Please explain why this shows such an error?

Can you please post complete code for the class?

Since 'sleep' is a static method of Thread class, to use it without class reference, you must access it from a class which IS-A Thread.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi ishu ,

You must be implement Runnable in this case,
Try "Thread.sleep(5000)", because the method sleep is defined in Thread class. OR use static import .

If you extends Thread then it should not be compiler error

Thanks,
Ankit

OCPJP 6 (91%)
sarvesh dikonda
Ranch Hand

Joined: Apr 08, 2012
Posts: 58

Ankit Gareta wrote:Hi ishu ,

You must be implement Runnable in this case,
Try "Thread.sleep(5000)", because the method sleep is defined in Thread class. OR use static import .

If you extends Thread then it should not be compiler error

Thanks,
Ankit


Its true try this, import static java.lang.Thread.*; and you will surel get the required output rather the error will no more exist


Always believe in yourself
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
Problem Solved. Thanks.
 
GeeCON Prague 2014
 
subject: Thread problem