This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 1502
    
    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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread problem
 
Similar Threads
Hibernate Bi Directional Problem
KAM India Edition Question 6.27
a doubt
SCJP2 Question - Compiler Error Vs. Run-time Error
Still can't write vector to file...?