Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am a beginner in Multi-Threading.

While doing a simple multi-threading program I get an error as follows:

.\Thread.java:24: cannot find symbolsymbol : method sleep(int)
location: class Thread
Thread.sleep(500);

^


I get the same error with Thread.start() function as well.
Can anybody help me to find a solution for the same?
Below is my program code:

  class NewThread implements Runnable {
   Thread t;
   NewThread() {
      // Create a new, second thread
      t = new Thread(this, "Demo Thread");
      System.out.println("Child thread: " + t);
      t.start(); // Start the thread
   }
      // This is the entry point for the second thread.
   public void run() {
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Child Thread: " + i);
            // Let the thread sleep for a while.
            Thread.sleep(500);
         }
     } catch (InterruptedException e) {
         System.out.println("Child interrupted.");
     }
     System.out.println("Exiting child thread.");
   }
}
class ThreadDemo1 {
   public static void main(String args[]) {
      new NewThread(); // create a new thread
      try {
         for(int i = 5; i > 0; i--) {
           System.out.println("Main Thread: " + i);
           Thread.sleep(1000);
         }
      } catch (InterruptedException e) {
         System.out.println("Main thread interrupted.");
      }
      System.out.println("Main thread exiting.");
   }
}
Posted
Updated 13-Oct-10 21:38pm
v2
Comments
Richard MacCutchan 14-Oct-10 8:06am    
Your comment below is correct; although you would have trouble starting any Java program if it was not set.

Are you sure that your classpath is set correctly? I have copied your code into my IDE (eclipse) and it runs fine in there and also from the command line.
 
Share this answer
 
Comments
vidhyaej 14-Oct-10 7:44am    
you mean the path which we set in the EnvironmentVariables ,isn't it?
C:\Program Files\Java\jdk1.6.0_21\bin .Am i correct?
Oh..my goodness....i had been trying out many programs and faced the same problem with all of them....

Now i got the reason.It may help some of you(<b>beginners like me</b>)

i had saved a file <b>'Thread.java' </b>unknowingly in the same folder and that created all these problems...i renamed it and all errors were rectified....

Thanks to Richard because he made me think of such a possibility (by asking whether the classpath is correct...actually the class refered to, in the error report was Thread.java which is actually a built in class in java.lang package )
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900