Java - 访问静态方法 Sleep - 出了什么问题?
当我将下面的代码放入 NetBeans 中时,NetBeans 会在旁边显示一条警告,提示“访问静态方法 sleep”。
try {
Thread.currentThread().sleep(2000);
}
catch(InterruptedException ie){
//continue
}
我做错了什么吗?我应该以不同的方式称呼它吗?我没有做任何多线程的事情。我只有一个简单的主要方法,我想睡一会儿。
When I put the code below in NetBeans, NetBeans gives me a warning next to it saying "Accessing static method sleep".
try {
Thread.currentThread().sleep(2000);
}
catch(InterruptedException ie){
//continue
}
Am I doing something wrong? Should I be calling this differently? I'm not doing anything multi-threaded. I just have a simple main method that which I want to sleep for a while.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Thread.currentThread() 返回 Thread 类的实例。当调用静态方法时,您只想对类本身进行操作。因此,在当前线程上调用静态方法时,您将收到一条警告,您正在实例上调用该方法。
您只需调用
Thread.sleep(2000);
它相当于Thread.currentThread.sleep(2000);
知道这一点很重要,因为人们已经被烧伤了类似:
编辑:那么我们怎么睡觉呢?您可以通过在传递给构造函数的 runnable 中编写 sleep 调用来休眠 a,例如:
当启动“a”时,someRunnable 的 run 方法中的 Thread.currentThread 是“a”线程实例。
Thread.currentThread() returns an instance of the Thread class. When invoking a static method you only want to work on the class itself. So invoking a static method on current thread you will get a warning you are calling the method on an instance.
You would just call
Thread.sleep(2000);
It would be equivalent toThread.currentThread.sleep(2000);
This is important to know because people have been burned doing something like:
Edit: So how do we sleep a? You sleep a by writing the sleep invocation within the runnable passed in to the constructor, like:
When 'a' is started, Thread.currentThread in someRunnable's run method is the 'a' thread instance.
sleep
是静态的,因此您可以使用Thread.sleep(2000);
访问它。它影响当前线程。来自javadoc:
这意味着您不能休眠另一个线程,只能休眠代码所在的线程。
sleep
is static, so you access it withThread.sleep(2000);
. It affects the current thread.From the javadoc:
What that means is that you can't sleep another thread, only the one that the code is in.
那是因为 sleep() 方法被声明为静态,因此
与
Thats because the sleep() method is declared as static therefore
is the same as
Thread 实例上没有与“sleep(long)”对应的方法。
Thread.currentThread().sleep(2000);但是,确实可以编译,因为线程类上有一个名为 sleep() 的方法,带有长参数。
Java 允许将此作为编译器时间技巧,以便新程序员在对方法的静态访问感到困惑时可以执行此操作。
然而,这实际上在编译器中解决的是:
Thread.sleep(2000);
以下代码也是等效的:
Thread t = new Thread(new Runnable() { public void run() { // 不执行任何操作 } });
t.睡眠(2000);
正如一位发帖者(John V)所指出的,这不会使实际的线程 (t) 实例休眠 - 创建线程对象的当前线程被置于休眠状态。
该警告的存在使您记住您正在访问类的静态方法,而不是实例变量的方法。
要编写的适当代码始终是 Thread.sleep(2000); - 切勿通过实例访问静态方法,以避免混淆和此警告。
There is no method on Thread instances that corresponds to "sleep(long)".
Thread.currentThread().sleep(2000); does compile, however, because there is a method on the thread class that is called sleep() with a long argument.
Java allows this as a compiler time trick so that new coders could execute this when they are confused about the static access of methods.
However, what this actually is resolved to in the compiler is:
Thread.sleep(2000);
The following code is also equivalent:
Thread t = new Thread(new Runnable() { public void run() { // do nothing } });
t.sleep(2000);
As one poster (John V) has pointed out, this doesn't make the actual thread (t) instance sleep - the current thread that created the thread object is put to sleep.
The warning exists such that you remember that you're accessing a static method of a class, not a method of an instance variable.
The appropriate code to write is always Thread.sleep(2000); - never access static methods through an instance to avoid confusion and this warning.
netbeans 向您发出警告,因为您正在从 Thread 引用而不是从 Thread 类访问静态方法。
尝试这个
sleep 方法导致当前正在执行的线程休眠。所以不需要调用 Thread.currentThread()。
netbeans giving you warning because you are accessing static method from Thread reference not from Thread class.
try this
sleep method Causes the currently executing thread to sleep.So no need to call Thread.currentThread().
每当您尝试使用对象访问静态方法时,这都不是最佳实践,NB当时会发出警告,这里是相同的情况
Thread.currentThread() 将返回 Thread 的对象
Whenever you try to access static method using object it is not best practise, NB gives warning at that time, here is the same case
Thread.currentThread() will return you a object of Thread