Java:为什么 Thread.sleep() 和 yield() 是静态的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17312048/
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 01:39:21  来源:igfitidea点击:

Java: why Thread.sleep() and yield() are static?

javamultithreading

提问by Rajan Garg

Why sleep()and yield()methods are defined as staticmethods in java.lang.Threadclass?

为什么sleep()yield()方法被定义为static方法java.lang.Threadclass

回答by Suresh Atta

The code would only execute when someXThreadwas executing, in which case telling someYThreadto yield would be pointless. So since the only thread worth calling yieldon is the current thread, they make the method staticso you won't waste time trying to call yieldon some other thread.

代码只会在执行时someXThread执行,在这种情况下告诉someYThreadyield 将毫无意义。如此以来,只有线程调用价值yield上是当前线程,它们使该方法static这样你就不会浪费时间去打电话yield其他线程

回答by Prasad Kharkar

This is because whenever you are calling these methods, those are applied on the same thread that is running. You can't tell another thread to perform some operation like, sleep()or wait. All the operation are performed on the thread which is being executed currently.

这是因为每当您调用这些方法时,它们都会应用到正在运行的同一线程上。您不能告诉另一个线程执行某些操作,例如,sleep()wait。所有操作都在当前正在执行的线程上执行。

回答by stinepike

If you call the yieldor sleepmethod, it applies to whichever thread is currently executing, rather than any specific thread - you don't have to specify which thread is currently running to free up the processor.

如果您调用yieldorsleep方法,它适用于当前正在执行的任何线程,而不是任何特定线程 - 您不必指定当前正在运行哪个线程来释放处理器。

similar thread in this forum

这个论坛里有类似的帖子

回答by Mikhail

The same reason is why stop() and suspend() methods are deprecated. Intrusion in thread's state from outside is dangerous and can cause unpredictable result. And if sleep is not static, for example, how do you think interruption from it will happen?

同样的原因是 stop() 和 suspend() 方法被弃用的原因。从外部侵入线程状态是危险的,并且会导致不可预知的结果。例如,如果睡眠不是静态的,您认为它会如何中断?

回答by SWAR0714

They are static so that overriding concept can be avoided i.e.

它们是静态的,因此可以避免覆盖概念,即

When they are called with parent class reference to hold child class object like situation it implements Method Hiding concept and not overriding due to Static method nature, i.e parent class(here thread class) method will run which have the complete functionality of sleep and yield.

当使用父类引用调用它们以保持子类对象的情况时,它实现了方法隐藏概念,并且由于静态方法性质而不会覆盖,即父类(此处为线程类)方法将运行,具有完整的 sleep 和 yield 功能。

http://i.stack.imgur.com/goygW.jpg

http://i.stack.imgur.com/goygW.jpg

回答by kartik kudada

Both sleep and yield methods are native. To understand better from above answers I made two classes ClassA and ClassB with same static method. I invoked method of other class to check its behavior. So we can call other class' static method.
So may be there is other reason behind making sleep method static.

sleep 和 yield 方法都是原生的。为了更好地理解上述答案,我使用相同的静态方法创建了两个类 ClassA 和 ClassB。我调用了其他类的方法来检查它的行为。所以我们可以调用其他类的静态方法。
因此,使 sleep 方法静态化可能还有其他原因。

public class ClassA {

    public static void method(){
        System.out.println("Inside ClassA method");
    }

    public static void main(String[] args) {
        method();
        ClassB classb = new ClassB();
        classb.method();
    }
}

    public class ClassB {
    public static void method(){
        System.out.println("Inside ClassB method");
    } 

}