• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread problem

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem Solved. Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic