静态休眠线程与使用其实例

发布于 2024-11-04 19:02:18 字数 289 浏览 0 评论 0原文

-

Thread workerThread = new Thread(runnable);
workerThread.start();
workerThread.sleep(5000);
.....

它们都会

Thread workerThread = new Thread(runnable);
workerThread.start();
Thread.sleep(5000);

导致工作线程暂停睡眠吗?

谢谢

What is the difference between -

Thread workerThread = new Thread(runnable);
workerThread.start();
workerThread.sleep(5000);
.....

And

Thread workerThread = new Thread(runnable);
workerThread.start();
Thread.sleep(5000);

Do they both cause worker thread to pause sleep?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

偏爱自由 2024-11-11 19:02:18

sleep 是影响当前线程的静态方法: http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#sleep(long)

所以没有区别在于,他们都不会做你想做的事。不建议在实例上使用静态方法,不推荐,因为它会让您认为您可以做一些实际上做不到的事情(就像在本例中一样)

小补充 - 请参阅 John 对 为什么调用静态Java 中允许使用类实例的方法 :-)

No sleep is a static method that affects the current thread: http://download.oracle.com/javase/6/docs/api/java/lang/Thread.html#sleep(long)

So there is no difference, they both won't do what you want. The use of static methods on an instance is deprecated discouraged since it make you think you can do something you actually can't (like in this case)

Small addition - see John's answer to why calling static methods from class instances is allowed in Java :-)

森林很绿却致人迷途 2024-11-11 19:02:18

两个实例中的 sleep 是相同的静态方法,它会导致当前正在执行的线程休眠,而不是工作线程休眠。

sleep in both instances is the same static method that would cause the currently executing thread to sleep, not the workerThread.

走过海棠暮 2024-11-11 19:02:18

唯一实际的调用是

Thread.sleep(5000);

另一个是java的奇怪之处,你可以通过实例变量调用静态方法。

您应该始终使用

Thread.sleep(5000);

它,因为它是更好的自记录,它是当前线程“始终”正在休眠。如果您这样做,

myThread.sleep(5000);

则您不是在休眠 myThread 线程,而是在休眠当前线程。

The only actual call is

Thread.sleep(5000);

The other is an oddity of java that you can call static method's through instance variables.

You should always use

Thread.sleep(5000);

As it is better self-documenting that it is the current thread 'always' that is being slept on. If you do

myThread.sleep(5000);

You are not sleeping the myThread thread, you are sleeping the current thread.

心碎的声音 2024-11-11 19:02:18

第二个方法应该是首选,因为 Thread.sleep 是一个静态方法,因此应该像所有静态方法一样,始终在定义它的类上而不是在此类的实例上调用。

The second one should be preferred, because Thread.sleep is a static method, and should thus always be invoked, like all static methods, on the class defining it rather than on an instance of this class.

灰色世界里的红玫瑰 2024-11-11 19:02:18

这是另一种可能性。使用易失性标志将消息传递给 C。

volatile boolean startC = false;

void B() {
  // Do initial B work.
  startC = true;  // Set the volatile flag.
  // Finish B processing not relevant to C.
}

void C() {
  // Wait for B to progress far enough.
  while (!startC) {
    Thread.sleep(100);
  }
  // B has done enough work so we can begin.
}

这不是真正的代码,足以让您了解我的意思。例如,您需要注意 startC 标志的可见性,或者为其编写一个公共 getter 和 setter,以便 B() 和 C() 都可以访问。

Here is another possibility. Use a volatile flag to pass the message to C.

volatile boolean startC = false;

void B() {
  // Do initial B work.
  startC = true;  // Set the volatile flag.
  // Finish B processing not relevant to C.
}

void C() {
  // Wait for B to progress far enough.
  while (!startC) {
    Thread.sleep(100);
  }
  // B has done enough work so we can begin.
}

This is not real code, just enough to give you the idea of what I am getting at. For example, you will need to pay attention to the visibility of the startC flag, or write a public getter and setter for it so B() and C() both have access.

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