Java:Thread.currentThread()。sleep(x)vs. Thread.sleep(x) [英] Java: Thread.currentThread().sleep(x) vs. Thread.sleep(x)

查看:234
本文介绍了Java:Thread.currentThread()。sleep(x)vs. Thread.sleep(x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Thread.currentThread()。sleep(x); 

Eclipse告诉我使用静态

 的Thread.sleep(X); 

为什么?有什么区别,这两种方法之间的功能有什么不同吗?

解决方案

只有一种方法,而不是两种,它是静态的。虽然您可以通过实例引用调用静态方法,但它不是很好的风格。它表示程序员认为他或她正在调用一个实例方法。一个困惑的程序员可能会认为他或她可能会导致另一个线程(而不是当前的)以这种方式睡觉,而不是这样。



代码做同样的事情,但第二个是更好的风格。


I have this in my code

Thread.currentThread().sleep(x);

Eclipse tells me to use the static

Thread.sleep(x); 

instead, why? What's the difference, is there some difference in functionality at all between these 2 methods?

解决方案

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.

这篇关于Java:Thread.currentThread()。sleep(x)vs. Thread.sleep(x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆