| 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: 1295
|
|
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)
|
 |
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.
|
 |
 |
|
|
subject: Thread problem
|
|
|