找不到符号:Thread.sleep(1000);

lea*_*ner 1 java multithreading

我想使用该Runnable接口实现线程.

我有以下三个类:

FirstThread.java

public class FirstThread implements Runnable
{  

  //This method will be executed when this thread is executed  
  public void run()    
  {                

    //Looping from 1 to 10 to display numbers from 1 to 10     
    for ( int i=1; i<=10; i++)      
    {          
        //Displaying the numbers from this thread          
        System.out.println( "Messag from First Thread : " +i);                     

       /*taking a delay of one second before displaying next number              
        *            
        * "Thread.sleep(1000);" - when this statement is executed,           
        * this thread will sleep for 1000 milliseconds (1 second)           
        * before executing the next statement.           
        *            
        * Since we are making this thread to sleep for one second,           
        * we need to handle "InterruptedException". Our thread           
        * may throw this exception if it is interrupted while it             
        * is sleeping.           
        *            
        */         
        try            
        {              
           Thread.sleep (1000);            
        }          
        catch (InterruptedException interruptedException)          
        {              
           /*Interrupted exception will be thrown when a sleeping or waiting
            *thread is interrupted.                  
            */             
            System.out.println( "First Thread is interrupted when it is sleeping" +interruptedException);          
        }  
    }  
  }
}
Run Code Online (Sandbox Code Playgroud)

SecondThread.java:

public class SecondThread implements Runnable
{  

   //This method will be executed when this thread is executed 
   public void run()   
   {           

      //Looping from 1 to 10 to display numbers from 1 to 10       
      for ( int i=1; i<=10; i++)        
      {            
         System.out.println( "Messag from Second Thread : " +i);                       

        /*taking a delay of one second before displaying next number             
         *           
         * "Thread.sleep(1000);" - when this statement is executed,              
         * this thread will sleep for 1000 milliseconds (1 second)           
         * before executing the next statement.              
         *          
         * Since we are making this thread to sleep for one second,              
         * we need to handle "InterruptedException". Our thread              
         * may throw this exception if it is interrupted while it            
         * is sleeping.              
         */            
         try           
         {             
             Thread.sleep(1000);           
         }         
         catch (InterruptedException interruptedException)         
         {             
            /*Interrupted exception will be thrown when a sleeping or waiting                
             * thread is interrupted.                
             */                
             System.out.println( "Second Thread is interrupted when it is sleeping" +interruptedException);            
         }     
      }    
    } 
}
Run Code Online (Sandbox Code Playgroud)

ThreadDemo.java:

public class ThreadDemo
{
     public static void main(String args[])
     {
        //Creating an object of the first thread
        FirstThread   firstThread = new FirstThread();

        //Creating an object of the Second thread
        SecondThread   secondThread = new SecondThread();

        //Starting the first thread
        Thread thread1 = new Thread(firstThread);
        thread1.start();

        //Starting the second thread
        Thread thread2 = new Thread(secondThread);
        thread2.start();
     }
} 
Run Code Online (Sandbox Code Playgroud)

在编译上面的程序时,我收到以下错误:

FirstThread

shahjahan@shahjahan-AOD270:~/Documents/java$ javac FirstThread.java 
FirstThread.java:28: error: cannot find symbol
           Thread.sleep (1000);            
                 ^
  symbol:   method sleep(int)
  location: class Thread
1 error
Run Code Online (Sandbox Code Playgroud)

SecondThread:

    shahjahan@shahjahan-AOD270:~/Documents/java$ javac SecondThread.java 
    SecondThread.java:26: error: cannot find symbol
                 Thread.sleep(1000);           
                       ^
      symbol:   method sleep(int)
      location: class Thread
    1 error
Run Code Online (Sandbox Code Playgroud)

ThreadDemo就:

shahjahan@shahjahan-AOD270:~/Documents/java$ javac ThreadDemo.java 
ThreadDemo.java:12: error: constructor Thread in class Thread cannot be applied to given types;
        Thread thread1 = new Thread(firstThread);
                         ^
  required: no arguments
  found: FirstThread
  reason: actual and formal argument lists differ in length
ThreadDemo.java:13: error: cannot find symbol
        thread1.start();
               ^
  symbol:   method start()
  location: variable thread1 of type Thread
ThreadDemo.java:16: error: constructor Thread in class Thread cannot be applied to given types;
        Thread thread2 = new Thread(secondThread);
                         ^
  required: no arguments
  found: SecondThread
  reason: actual and formal argument lists differ in length
ThreadDemo.java:17: error: cannot find symbol
        thread2.start();
               ^
  symbol:   method start()
  location: variable thread2 of type Thread
./FirstThread.java:28: error: cannot find symbol
           Thread.sleep (1000);            
                 ^
  symbol:   method sleep(int)
  location: class Thread
./SecondThread.java:26: error: cannot find symbol
             Thread.sleep(1000);           
                   ^
  symbol:   method sleep(int)
  location: class Thread
6 errors
Run Code Online (Sandbox Code Playgroud)

我是java的新手.为什么thread.sleep不起作用.线程实现是否依赖于编译它的机器?

rol*_*lfl 6

要么导入一些具有名称的类,要么Thread不是java.lang.Thread,或者Thread在当前代码目录中调用的类是"污染"类的导入.

删除/重命名文件夹中名为"Thread"的任何类,并删除所调用的类的任何导入 Thread