2014-10-07 5 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(); 
    } 
} 

위의 컴파일에 홍보

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 

내가 자바 초보자입니다 프로그램 RL FR RR FL, 나는 다음과 같은 오류를 얻었다. thread.sleep이 작동하지 않는 이유는 무엇입니까? 스레드 구현은 컴파일되는 머신에 달려 있습니까?

+6

클래스 본문뿐만 아니라 모든 'import'문장을 표시해야합니다. 내 기대는 당신이 다른'Thread' 클래스를 임포트하고 있다는 것이다. – rolfl

+2

어쨌든 – soulcheck

+1

@learner라고 정의 된 자신 만의'Thread' 클래스를 가지고 있는지, 가장 먼저 발생한 에러를 확인하십시오. 이것이 문제입니다. 힌트는 다음과 같습니다. ThreadDemo.java의 12 행. – bedwyr

답변

6

이름이 Thread 인 일부 클래스를 가져 오거나 java.lang.Thread이 아니거나 현재 코드 디렉토리에 Thread이라는 클래스가있어 클래스에 대한 가져 오기를 오염시키고 있습니다.

제거/귀하의 폴더에 '스레드'라는 어떤 클래스의 이름을 변경하고 코드에 문제가 없습니다 Thread

2

라는 클래스에 대한 모든 수입을 제거, 나는 그것을 테스트하고 그것을 잘 일하고있어.

프로젝트에서 Thread라는 클래스가 있으면 제거하거나 이름을 바꾸고 다시 시도하십시오. 충돌하는 Thread 클래스가있을 수 있습니다.

+0

네가 맞습니다. 스레드 클래스를 정의했는지 여부를 어떻게 알 수 있습니까? – learner

+0

import 문을 살펴보고 – Josh

+1

@learner를 사용하는 "스레드"를 볼 수 있습니다. IDE (Eclipse, Netbeans, IntelliJ 등)를 사용하는 경우 Eclipse에서 쉽게 사용할 수 있습니다 'Thread'라는 단어를 선택하고 'f3'을 누르십시오. – rolfl

관련 문제