Java - 访问静态方法 Sleep - 出了什么问题?

发布于 2024-09-13 08:02:08 字数 302 浏览 5 评论 0原文

当我将下面的代码放入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

机场等船 2024-09-20 08:02:08

Thread.currentThread() 返回 Thread 类的实例。当调用静态方法时,您只想对类本身进行操作。因此,在当前线程上调用静态方法时,您将收到一条警告,您正在实例上调用该方法。

您只需调用 Thread.sleep(2000); 它相当于 Thread.currentThread.sleep(2000);

知道这一点很重要,因为人们已经被烧伤了类似:

Thread a = new Thread(someRunnable);

a.start();
a.sleep(2000);  //this will sleep the current thread NOT a.

编辑:那么我们怎么睡觉呢?您可以通过在传递给构造函数的 runnable 中编写 sleep 调用来休眠 a,例如:

Runnable someRunnable = new Runnable(){
    public void run(){
        Thread.sleep(2000);
    }
};

当启动“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 to Thread.currentThread.sleep(2000);

This is important to know because people have been burned doing something like:

Thread a = new Thread(someRunnable);

a.start();
a.sleep(2000);  //this will sleep the current thread NOT a.

Edit: So how do we sleep a? You sleep a by writing the sleep invocation within the runnable passed in to the constructor, like:

Runnable someRunnable = new Runnable(){
    public void run(){
        Thread.sleep(2000);
    }
};

When 'a' is started, Thread.currentThread in someRunnable's run method is the 'a' thread instance.

云醉月微眠 2024-09-20 08:02:08

sleep 是静态的,因此您可以使用 Thread.sleep(2000); 访问它。它影响当前线程。

来自javadoc:

导致当前正在执行的线程
睡眠(暂时停止执行)
对于指定数量的
毫秒。线不丢失
任何监视器的所有权。

这意味着您不能休眠另一个线程,只能休眠代码所在的线程。

sleep is static, so you access it with Thread.sleep(2000);. It affects the current thread.

From the javadoc:

Causes the currently executing thread
to sleep (temporarily cease execution)
for the specified number of
milliseconds. The thread does not lose
ownership of any monitors.

What that means is that you can't sleep another thread, only the one that the code is in.

丑疤怪 2024-09-20 08:02:08

那是因为 sleep() 方法被声明为静态,因此

Thread.currentThread().sleep(2000);

Thread.sleep(2000);

Thats because the sleep() method is declared as static therefore

Thread.currentThread().sleep(2000);

is the same as

Thread.sleep(2000);
热风软妹 2024-09-20 08:02:08

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.

老娘不死你永远是小三 2024-09-20 08:02:08

netbeans 向您发出警告,因为您正在从 Thread 引用而不是从 Thread 类访问静态方法。
尝试这个

try {
            Thread.sleep(2000);
        }
        catch(InterruptedException ie){
            //continue
        }

sleep 方法导致当前正在执行的线程休眠。所以不需要调用 Thread.currentThread()。

netbeans giving you warning because you are accessing static method from Thread reference not from Thread class.
try this

try {
            Thread.sleep(2000);
        }
        catch(InterruptedException ie){
            //continue
        }

sleep method Causes the currently executing thread to sleep.So no need to call Thread.currentThread().

盗梦空间 2024-09-20 08:02:08

每当您尝试使用对象访问静态方法时,这都不是最佳实践,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文