java - Thread 类中的构造函数 Thread 不能应用于给定类型;

标签 java java-threads

为什么它显示错误,因为我正在从“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: "duplicate class" and Mismatched File Name Error

关于java - Thread 类中的构造函数 Thread 不能应用于给定类型;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57352427/

相关文章:

java - 我们如何使用任何 Java API 将 WMF/EMF(MS 图元文件)转换为 JPG 或 PNG 等标准图像?

java - 从内部类的对象访问外部类函数

java - 仅在 ExecutorService 中提交任务时设置变量值

java - 方法引用如何与线程一起使用? `new Thread(foo::bar)`

android - wait()函数在android studio中的线程中不起作用

java - 针对目标用户和组的 Azure 应用程序配置功能标记不起作用

java - Wicket 口中带有文件下载的提交按钮

java - 使用 Morphia 映射 MongoDB 中的层次结构对象

Java:停止没有同步的线程

java - 将工作线程与主线程一起使用会减少基于 Java 的智能设备的响应延迟或增加工作负载吗?