2014-10-07 34 views
1

我想使用Runnable接口实现线程。找不到符号:Thread.sleep(1000);

我有以下三类:

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);   
     } 
    } 
    } 
} 

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);    
     }  
     }  
    } 
} 

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(); 
    } 
} 

在上面编译PR ograms,我得到了以下错误:

FirstThread

[email protected]:~/Documents/java$ javac FirstThread.java 
FirstThread.java:28: error: cannot find symbol 
      Thread.sleep (1000);    
       ^
    symbol: method sleep(int) 
    location: class Thread 
1 error 

SecondThread:

[email protected]:~/Documents/java$ javac SecondThread.java 
    SecondThread.java:26: error: cannot find symbol 
       Thread.sleep(1000);   
        ^
     symbol: method sleep(int) 
     location: class Thread 
    1 error 

ThreadDemo就:

[email protected]:~/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 

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

+6

你需要向我们展示你所有的'import'语句,而不仅仅是类的主体。我的期望是你正在导入一些其他的'Thread'类。 – rolfl 2014-10-07 14:24:29

+2

很确定你有自己的'Thread'类定义了某处 – soulcheck 2014-10-07 14:25:18

+1

@learner,检查第一个错误 - 这就是你的问题。这里有一个提示:ThreadDemo.java的第12行。 – bedwyr 2014-10-07 14:25:32

回答

6

要么你正在导入一些名称为Thread的类,那不是java.lang.Thread,或者在你的当前代码目录中有一个名为Thread的类,它正在'污染'你的类的导入。

删除/重命名叫你的文件夹中的“线”的任何类,并移除任何进口名为Thread

2

类没有与你的代码没有问题,我测试了它和它的正常工作。

如果您的项目中有任何称为线程的类删除或重命名它们,并且再次尝试,那么您可能有一个冲突的Thread类。

+0

是的你是对的。我怎么知道我是否定义了一个线程类? – learner 2014-10-07 14:34:24

+0

看看你的导入语句,你会看到它使用的是哪个“Thread” – Josh 2014-10-07 14:37:10

+1

@learner - 如果你使用IDE(Eclipse,Netbeans,IntelliJ等),很容易,例如在Eclipse中将光标置于'Thread'这个词,然后按'f3' – rolfl 2014-10-07 14:37:49