我想使用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不起作用。线程的实现是否依赖于它所编译的机器?
你需要向我们展示你所有的'import'语句,而不仅仅是类的主体。我的期望是你正在导入一些其他的'Thread'类。 – rolfl 2014-10-07 14:24:29
很确定你有自己的'Thread'类定义了某处 – soulcheck 2014-10-07 14:25:18
@learner,检查第一个错误 - 这就是你的问题。这里有一个提示:ThreadDemo.java的第12行。 – bedwyr 2014-10-07 14:25:32