Java -类型线程的方法sleep(int)是未定义的

时间:2021-07-09 00:40:29

I'm getting an error: The method sleep(int) is undefined for the type Thread. I thought the sleep method is in the Thread class in Java.

我得到一个错误:方法sleep(int)没有为类型线程定义。我认为sleep方法在Java的Thread类中。

import java.util.Random;

public class Thread implements Runnable {

    String name;
    int time;
    Random r = new Random();

    public Thread(String s){
        name = s;
        time = r.nextInt(999);
    }

    public void run() {
        try{
            System.out.printf("%s is sleeping for %d\n", name, time);
            Thread.sleep(time);
            System.out.printf("%s is done", name);
        } catch(Exception e ) {
        }
    }
}

7 个解决方案

#1


18  

You implemented your own class called Thread and try to call sleep on it, which fails, cause sleep is undefined in your class. Your class basically shadows java's Thread class.

您实现了自己的类Thread并尝试调用它的sleep,但失败了,因为您的类中没有定义sleep。您的类基本上是阴影java的线程类。

Call your class differently (ie. MyThread or even better MyRunnable, as noted by owlstead) or call java.lang.Thread.sleep() directly.

用不同的方式称呼你的班级。如owlstead所指出的,MyThread或更好的MyRunnable,或者直接调用java.lang.Thread.sleep()。

#2


5  

It's not in your Thread class.

它不在线程类中。

Since you named your class Thread, that's where Java will look for Thread.sleep. If you want the function that's built into Java, try java.lang.Thread.sleep(time);.

既然您给类命名了Thread,那么Java就会在这里查找Thread.sleep。如果您想要构建到Java中的函数,请尝试Java .lang. thread .sleep(time);。

#3


2  

Your class name "Thread" conflicts with the Thread class in Java standard library. Change the name of your class and it will resolve everything.

类名“Thread”与Java标准库中的Thread类发生冲突。更改类的名称,它将解决所有问题。

#4


2  

The reason you're getting this is that you've implemented your own Thread class. In your class there is no sleep method.

得到这个的原因是您实现了自己的线程类。在你的课上没有睡觉的方法。

First prize would be to avoid using class names that are part of the standard Java libraries.

首先要避免使用作为标准Java库一部分的类名。

If you insists to keep the names, use java.lang.Thread.sleep(...) to specify that you want the Thread class that Java provides.

如果您坚持保留名称,请使用Java .lang.Thread.sleep(…)来指定您想要Java提供的线程类。

#5


1  

Fully-qualify Thread since you're trying to use java.lang.Thread, not your own.

完全限定线程,因为您正在尝试使用java.lang。线程,而不是你自己的。

#6


1  

The problem is that your class is named Thread, which doesn't have a sleep() method. The sleep method is in java.lana.Thread, which is being hidden by your class.

问题是您的类名为Thread,它没有sleep()方法。睡眠方法在java.lana中。线程,它被类隐藏。

#7


0  

Answers to this question have already been posted yet one another analogy. It may not be the direct answer but may help you remove your confusion why should avoid reusing the names of platform classes, and never reuse class names from java.lang, because these names are automatically imported everywhere. Programmers are used to seeing these names in their unqualified form and naturally assume that these names refer to the familiar classes from java.lang. If you reuse one of these names, the unqualified name will refer to the new definition any time it is used inside its own package.

这个问题的答案已经被贴上了另一个类比。这可能不是直接的答案,但可以帮助您消除为什么应该避免重用平台类的名称,并且永远不要重用java中的类名称的困惑。lang,因为所有地方都自动导入这些名称。程序员习惯于以不合格的形式看到这些名称,并自然地假定这些名称引用java.lang中熟悉的类。如果重用这些名称中的一个,那么无论何时在自己的包中使用新定义,都将引用不合格的名称。


One name can be used to refer to multiple classes in different packages. The following simple code snippet explores what happens when you reuse a platform class name. What do you think it does? Look at it. It reuses the String class from the java.lang package. Give it a try.

一个名称可以用于引用不同包中的多个类。下面的简单代码片段探讨了当您重用一个平台类名时会发生什么。你认为它是做什么的?看它。它重用java中的String类。朗包。试一试。

package test;

final class String
{
    private final java.lang.String s;

    public String(java.lang.String s)
    {
        this.s = s;
    }

    @Override
    public java.lang.String toString()
    {
        return s;
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        String s = new String("Hello world");
        System.out.println(s);
    }
}

This program looks simple enough, if a bit repulsive. The class String in the unnamed package is simply a wrapper for a java.lang.String instance. It seems the program should print Hello world. If you tried to run the program, though, you found that you could not. The VM emits an error message something like this:

这个程序看起来很简单,如果有点排斥的话。未命名包中的类字符串只是java.lang的包装器。字符串实例。看起来程序应该打印Hello world。如果你试着运行这个程序,你会发现你做不到。VM会发出类似这样的错误消息:

Exception in thread "main" java.lang.NoSuchMethodError: main

If you're using the NetBeans IDE, the program would simply be prevented from running. You would receive the message No main classes found.

如果您使用的是NetBeans IDE,那么程序就会被简单地阻止运行。您将收到未找到主类的消息。


The VM can’t find the main method because it isn’t there. Although Main has a method named main, it has the wrong signature. A main method must accept a single argument that is an array of strings. What the VM is struggling to tell us is that Main.main accepts an array of our String class, which has nothing whatsoever to do with java.lang.String.

VM无法找到主方法,因为它不在那里。虽然Main有一个名为Main的方法,但它有错误的签名。主方法必须接受单个参数,即字符串数组。VM努力告诉我们的是主要的。main接收我们的String类的数组,它与java.lang.String没有任何关系。


Conclusion : As mentioned above, always avoid reusing platform class names from the java.lang package.

结论:如上所述,始终避免重用java中的平台类名。朗包。

#1


18  

You implemented your own class called Thread and try to call sleep on it, which fails, cause sleep is undefined in your class. Your class basically shadows java's Thread class.

您实现了自己的类Thread并尝试调用它的sleep,但失败了,因为您的类中没有定义sleep。您的类基本上是阴影java的线程类。

Call your class differently (ie. MyThread or even better MyRunnable, as noted by owlstead) or call java.lang.Thread.sleep() directly.

用不同的方式称呼你的班级。如owlstead所指出的,MyThread或更好的MyRunnable,或者直接调用java.lang.Thread.sleep()。

#2


5  

It's not in your Thread class.

它不在线程类中。

Since you named your class Thread, that's where Java will look for Thread.sleep. If you want the function that's built into Java, try java.lang.Thread.sleep(time);.

既然您给类命名了Thread,那么Java就会在这里查找Thread.sleep。如果您想要构建到Java中的函数,请尝试Java .lang. thread .sleep(time);。

#3


2  

Your class name "Thread" conflicts with the Thread class in Java standard library. Change the name of your class and it will resolve everything.

类名“Thread”与Java标准库中的Thread类发生冲突。更改类的名称,它将解决所有问题。

#4


2  

The reason you're getting this is that you've implemented your own Thread class. In your class there is no sleep method.

得到这个的原因是您实现了自己的线程类。在你的课上没有睡觉的方法。

First prize would be to avoid using class names that are part of the standard Java libraries.

首先要避免使用作为标准Java库一部分的类名。

If you insists to keep the names, use java.lang.Thread.sleep(...) to specify that you want the Thread class that Java provides.

如果您坚持保留名称,请使用Java .lang.Thread.sleep(…)来指定您想要Java提供的线程类。

#5


1  

Fully-qualify Thread since you're trying to use java.lang.Thread, not your own.

完全限定线程,因为您正在尝试使用java.lang。线程,而不是你自己的。

#6


1  

The problem is that your class is named Thread, which doesn't have a sleep() method. The sleep method is in java.lana.Thread, which is being hidden by your class.

问题是您的类名为Thread,它没有sleep()方法。睡眠方法在java.lana中。线程,它被类隐藏。

#7


0  

Answers to this question have already been posted yet one another analogy. It may not be the direct answer but may help you remove your confusion why should avoid reusing the names of platform classes, and never reuse class names from java.lang, because these names are automatically imported everywhere. Programmers are used to seeing these names in their unqualified form and naturally assume that these names refer to the familiar classes from java.lang. If you reuse one of these names, the unqualified name will refer to the new definition any time it is used inside its own package.

这个问题的答案已经被贴上了另一个类比。这可能不是直接的答案,但可以帮助您消除为什么应该避免重用平台类的名称,并且永远不要重用java中的类名称的困惑。lang,因为所有地方都自动导入这些名称。程序员习惯于以不合格的形式看到这些名称,并自然地假定这些名称引用java.lang中熟悉的类。如果重用这些名称中的一个,那么无论何时在自己的包中使用新定义,都将引用不合格的名称。


One name can be used to refer to multiple classes in different packages. The following simple code snippet explores what happens when you reuse a platform class name. What do you think it does? Look at it. It reuses the String class from the java.lang package. Give it a try.

一个名称可以用于引用不同包中的多个类。下面的简单代码片段探讨了当您重用一个平台类名时会发生什么。你认为它是做什么的?看它。它重用java中的String类。朗包。试一试。

package test;

final class String
{
    private final java.lang.String s;

    public String(java.lang.String s)
    {
        this.s = s;
    }

    @Override
    public java.lang.String toString()
    {
        return s;
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        String s = new String("Hello world");
        System.out.println(s);
    }
}

This program looks simple enough, if a bit repulsive. The class String in the unnamed package is simply a wrapper for a java.lang.String instance. It seems the program should print Hello world. If you tried to run the program, though, you found that you could not. The VM emits an error message something like this:

这个程序看起来很简单,如果有点排斥的话。未命名包中的类字符串只是java.lang的包装器。字符串实例。看起来程序应该打印Hello world。如果你试着运行这个程序,你会发现你做不到。VM会发出类似这样的错误消息:

Exception in thread "main" java.lang.NoSuchMethodError: main

If you're using the NetBeans IDE, the program would simply be prevented from running. You would receive the message No main classes found.

如果您使用的是NetBeans IDE,那么程序就会被简单地阻止运行。您将收到未找到主类的消息。


The VM can’t find the main method because it isn’t there. Although Main has a method named main, it has the wrong signature. A main method must accept a single argument that is an array of strings. What the VM is struggling to tell us is that Main.main accepts an array of our String class, which has nothing whatsoever to do with java.lang.String.

VM无法找到主方法,因为它不在那里。虽然Main有一个名为Main的方法,但它有错误的签名。主方法必须接受单个参数,即字符串数组。VM努力告诉我们的是主要的。main接收我们的String类的数组,它与java.lang.String没有任何关系。


Conclusion : As mentioned above, always avoid reusing platform class names from the java.lang package.

结论:如上所述,始终避免重用java中的平台类名。朗包。