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이 작동하지 않는 이유는 무엇입니까? 스레드 구현은 컴파일되는 머신에 달려 있습니까?
클래스 본문뿐만 아니라 모든 'import'문장을 표시해야합니다. 내 기대는 당신이 다른'Thread' 클래스를 임포트하고 있다는 것이다. – rolfl
어쨌든 – soulcheck
@learner라고 정의 된 자신 만의'Thread' 클래스를 가지고 있는지, 가장 먼저 발생한 에러를 확인하십시오. 이것이 문제입니다. 힌트는 다음과 같습니다. ThreadDemo.java의 12 행. – bedwyr