Java:Thread.currentThread().sleep(x) 与 Thread.sleep(x)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2077216/
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
Java: Thread.currentThread().sleep(x) vs. Thread.sleep(x)
提问by Omu
I have this in my code
我的代码中有这个
Thread.currentThread().sleep(x);
Eclipse tells me to use the static
Eclipse 告诉我使用静态
Thread.sleep(x);
instead, why? What's the difference, is there some difference in functionality at all between these 2 methods?
相反,为什么?有什么区别,这两种方法在功能上有什么区别吗?
采纳答案by Sean Owen
There is only one method, not two, and it is static. While you can call a static method via an instance reference, it's not good style. It indicates the programmer thinks he or she is calling an instance method. A confused programmer might be thinking he or she can cause another thread (not the current one) to sleep this way, when that's not what it does.
只有一种方法,而不是两种,而且它是静态的。虽然您可以通过实例引用调用静态方法,但这不是一种好风格。它表明程序员认为他或她正在调用一个实例方法。一个困惑的程序员可能会认为他或她可以让另一个线程(不是当前线程)以这种方式休眠,但事实并非如此。
Both your lines of code do the same thing but the second is better style.
你的两行代码做同样的事情,但第二行是更好的风格。
回答by Mark Byers
In Java, sleep is a static method. Both your examples do exactly the same thing, but the former version is confusing because it looks like it is calling a method on a specific object but it's not doing that at all. In your example it won't matter much, but it is more dangerous if you have the following:
在 Java 中, sleep 是一个静态方法。您的两个示例都做完全相同的事情,但前一个版本令人困惑,因为它看起来像是在特定对象上调用方法,但根本没有这样做。在您的示例中,这无关紧要,但是如果您具有以下内容,则更危险:
someOtherThread.sleep(x);
This time it looks like you are telling some other thread to sleep, but in fact you are putting the current thread to sleep. The way to avoid making this type of mistake is to always call static methods using the class rather than a specific object.
这次看起来你是在告诉其他线程休眠,但实际上你是在让当前线程休眠。避免犯此类错误的方法是始终使用类而不是特定对象调用静态方法。
回答by Amir Moghimi
The two method calls are identical in behavior because they invoke the same method but using the class name (Threadin this case) rather than instance for accessing static fields and methods makes this static-ness clear. That is why this warning is produced.
这两个方法调用在行为上是相同的,因为它们调用相同的方法,但使用类名(在本例中为Thread)而不是用于访问静态字段和方法的实例,使这种静态性变得清晰。这就是产生此警告的原因。
But considering that static fields and methods are shown in a particular way in most IDEs (for example in italic font in Eclipse and IntelliJ IDEA), is this warning still necessary? Maybe not as much necessary as it was in the early days of Java that simple editors were in use.
但是考虑到静态字段和方法在大多数 IDE 中以特定方式显示(例如在 Eclipse 和 IntelliJ IDEA 中以斜体显示),这个警告是否仍然必要?也许不像 Java 早期使用简单编辑器那样必要。
回答by Dev Anand Sadasivam
Thread.currentThread().sleep(x);
or the way Eclipsesays so Thread.sleep(x);
static context is required if it is in the need, so we hold on for slight delay with this sleep.
Thread.currentThread().sleep(x);
或者Eclipse所说的方式,Thread.sleep(x);
如果需要,则需要静态上下文,因此我们在此睡眠中稍作延迟。
Static paradigm set by one object, only affects that particular object heap print life cycle, again considering the overall Object Life Cycle static is not that bothersome, if required it can be used to ease the coding, but to be done carefully as static foot-print is referred by Class
(for example:- Class.forName(pkg.className)
) as like by name and not by any object
which is run-time single print copy of Class in HEAP
memory.
一个对象设置的静态范式,只影响那个特定对象的堆打印生命周期,再次考虑到整个对象生命周期静态不是那么麻烦,如果需要它可以用来简化编码,但要像静态脚一样小心地完成- print 由Class
(例如:- Class.forName(pkg.className)
)按名称引用,而不是由任何object
类在HEAP
内存中的运行时单一打印副本引用。
Again usage of object also has pros & cons by Weak, Phantom, Strong kind of references....,
再次使用对象也有弱,幻影,强类型的引用的优点和缺点......,
Code is Convoluted by Nature. It is just the way how we do in order to make it work & functional.
代码天生就是令人费解的。这只是我们如何使其工作和功能正常的方式。