我想使用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);
}
}
}
}
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
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
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
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
我是java的新手。为什么thread.sleep不起作用。线程实现是否依赖于编译它的机器?
答案 0 :(得分:6)
要么导入名为Thread
的某个类,而不是java.lang.Thread
,要么当前代码目录中有一个名为Thread
的类,即污染& #39;你班级的进口。
删除/重命名任何名为&#39; Thread&#39;在您的文件夹中,删除名为Thread
答案 1 :(得分:2)
您的代码没有问题,我测试过它并且运行正常。
你可能有一个冲突的Thread类,如果你的项目中有任何名为Thread的类删除或重命名它们,然后重试。