java 类实现了 Runnable 但没有定义 start() 和 sleep() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17026272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:45:16  来源:igfitidea点击:

Class implements Runnable but start() and sleep() methods are not defined

javamultithreadinginterfacesleeprunnable

提问by user1729250

I've created a class called Threadthat implements Runnablebut I cannot invoke the start()or sleep()methods for some reason. Any time I attempt to do so, I get errors saying that these methods are undefined for the class and suggests that I create them. So I created a new project and copied a sample code to see if there was something wrong with my own code and I received the same errors. Here's the sample code:

我创建了一个名为Threadimplements的类,Runnable但由于某种原因我无法调用start()orsleep()方法。每当我尝试这样做时,我都会收到错误消息,指出这些方法未为类定义,并建议我创建它们。所以我创建了一个新项目并复制了一个示例代码来查看我自己的代码是否有问题并且我收到了相同的错误。这是示例代码:

class Thread implements Runnable {
    private int a;
    public Thread (int a) {
        this.a = a;
    }
    public void run() {
        for (int i = 1; i <= a; ++i) {
            System.out.println(Thread.currentThread().getName() + " is " + i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
        }
    }
}

and this is my own code:

这是我自己的代码:

public class Thread extends PID implements Runnable {
    public Thread() {};                      // Empty constructor for thread object
    public void run() {
        Random gen = new Random();           // Generates random values
        int sleepTime;                       // Sleep time
        sleepTime = gen.nextInt(60 - 1) + 1; // Generates random sleep time between 1 and 60 seconds
        try {
            Thread.sleep();
        } catch (Exception e) {
            System.out.println(e);
        } 
        System.out.println("The thread has been terminated");
    }
}

回答by jlordo

To fix your current error, simply rename your Threadclass to MyThread(or whatever), because your Threadclass is hiding the java.lang.Threadclass.

要修复您当前的错误,只需将您的Thread类重命名为MyThread(或其他名称),因为您的Thread类隐藏了java.lang.Thread该类。

If you want to stick to Thread, you'll have to use the fully qualified name for java.lang.Threadlike this:

如果您想坚持使用Thread,则必须使用完全限定名称,java.lang.Thread如下所示:

try{
    java.lang.Thread.sleep(1000);
    // ...

回答by Saurabh Rana

Mistakes

错误

  1. Change class name from Threadto MyThread.
  2. run()is called when you invoke start(). Invoke start()using class object.
  3. Thread.sleep();needs an argument say, sleepTime
  1. 将类名从 更改ThreadMyThread
  2. run()调用时调用start()start()使用类对象调用。
  3. Thread.sleep();需要论证说, sleepTime

Here is the code of what I think you want to do. Tested on Eclipse Juno JDK 1.7

这是我认为你想要做的代码。在 Eclipse Juno JDK 1.7 上测试

import java.util.Random;

class NewThread implements Runnable {

    public NewThread () {
    }
    public void run() {
        Random gen = new Random();           // Generates random values
        int sleepTime;                       // Sleep time
        sleepTime = gen.nextInt(60 - 1) + 1; // Generates random sleep time between 1 and 60 seconds

        try {
            Thread.sleep(sleepTime);
        } catch (Exception e) {
            System.out.println(e);
        } 
        System.out.println("The thread has been terminated");
    }
}

class MyThread {

    public static void main(String[] args) {
        NewThread t = new NewThread();
        Thread t1 = new Thread(t);
        t1.start();

    }
}

Output

输出

The thread has been terminated

线程已终止

回答by user3902800

Interface Runnable do not have methods start() and sleep(), so be carefull with it. Interface Runnable only have run() method, and Java API recommends " the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods." See Java API's Runnable documentation here: http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html
In every other cases, your class should extned class Thread.

接口 Runnable 没有方法 start() 和 sleep(),所以要小心使用它。接口 Runnable 只有 run() 方法,Java API 建议“如果您只打算覆盖 run() 方法而不打算覆盖其他 Thread 方法,则应使用 Runnable 接口。” 在此处查看 Java API 的 Runnable 文档:http:
//docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html 在所有其他情况下,您的类应该扩展类 Thread。