为什么它显示错误,因为我正在从“Head First with Java”一书中执行此操作。这是我使用过的代码:
import java.util.*;
import java.lang.*;
public class mallu{
public static void main(String[] args) {
Runnable threadJob = new MyRunnable();
Thread myThread = new Thread(threadJob);
myThread.start();
System.out.println("back in main");
}
}
class MyRunnable implements Runnable{
public void run(){
go();
}
public void go(){
doMore();
}
public void doMore(){
System.out.println("top o' the stack");
}
}
这就是我遇到的错误。
<小时/>./Thread.java:14: error: duplicate class: MyRunnable
class MyRunnable implements Runnable{
^
mallu.java:8: error: constructor Thread in class Thread cannot be applied to given types;
Thread myThread = new Thread(threadJob);
^
required: no arguments
found: Runnable
reason: actual and formal argument lists differ in length
mallu.java:9: error: cannot find symbol
myThread.start();
^
symbol: method start()
location: variable myThread of type Thread
./Thread.java:7: error: constructor Thread in class Thread cannot be applied to given types;
Thread myThread = new Thread(threadJob);
^
required: no arguments
found: Runnable
reason: actual and formal argument lists differ in length
./Thread.java:8: error: cannot find symbol
myThread.start();
^
symbol: method start()
location: variable myThread of type Thread
5 errors
我不明白为什么会发生这个错误。任何帮助将不胜感激。
最佳答案
./Thread.java:14: error: duplicate class: MyRunnable
在您的项目中看起来您有多个类名MyRunnable
要解决此问题,请将类名从 MyRunnable 更改为其他类名。
查看结果:Your code
关于java - Thread 类中的构造函数 Thread 不能应用于给定类型;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57352427/