【发布时间】:2011-04-01 23:38:08
【问题描述】:
sleep() 是 Thread 类的静态方法。从多个线程调用时它是如何工作的。以及它如何确定当前的执行线程。 ?
或者可能是一个更通用的问题是如何从不同的线程调用静态方法?不会有并发问题吗?
【问题讨论】:
标签: java multithreading static synchronization
sleep() 是 Thread 类的静态方法。从多个线程调用时它是如何工作的。以及它如何确定当前的执行线程。 ?
或者可能是一个更通用的问题是如何从不同的线程调用静态方法?不会有并发问题吗?
【问题讨论】:
标签: java multithreading static synchronization
sleep 方法使当前线程休眠,因此如果您从多个线程调用它,它将使每个线程休眠。还有currentThread 静态方法可以让你获取当前正在执行的线程。
【讨论】:
Thread.sleep(long) 在 java.lang.Thread 类中本地实现。这是其 API 文档的一部分:
Causes the currently executing thread to sleep (temporarily cease
execution) for the specified number of milliseconds, subject to
the precision and accuracy of system timers and schedulers. The thread
does not lose ownership of any monitors.
sleep 方法使调用它的线程休眠。(基于 EJP 的 cmets)确定当前执行的线程(调用它并使其休眠)。 Java 方法可以确定哪个线程正在执行通过调用Thread.currentThread()
方法(静态或非静态)可以同时从任意数量的线程调用。只要你的方法是thread safe,就不会有任何并发问题。 只有当多个线程在没有适当同步的情况下修改类或实例的内部状态时,您才会遇到问题。
【讨论】:
一个更通用的问题是如何从不同的线程调用静态方法?不会有并发问题吗?
如果一个或多个线程修改共享状态,而另一个线程使用相同的状态,则只有潜在的并发问题。 sleep() 方法没有共享状态。
【讨论】:
它是如何计算当前的 执行线程?
没必要。它只是调用操作系统,而操作系统总是让调用它的线程休眠。
【讨论】:
当虚拟机遇到sleep(long)-statement时,会中断当前运行的Thread。那一刻的“当前线程”始终是调用Thread.sleep() 的线程。然后它说:
嘿!在这个线程中无事可做(因为我必须等待)。我将继续另一个线程。
改变线程被称为“屈服”。 (注:您可以拨打Thread.yield();自行让步)
所以,它不必弄清楚当前线程是什么。调用 sleep() 的始终是线程。
注意:可以通过调用Thread.currentThread();获取当前线程
一个简短的例子:
// here it is 0 millis
blahblah(); // do some stuff
// here it is 2 millis
new Thread(new MyRunnable()).start(); // We start an other thread
// here it is 2 millis
Thread.sleep(1000);
// here it is 1002 millis
MyRunnable 其run() 方法:
// here it is 2 millis; because we got started at 2 millis
blahblah2(); // Do some other stuff
// here it is 25 millis;
Thread.sleep(300); // after calling this line the two threads are sleeping...
// here it is 325 millis;
... // some stuff
// here it is 328 millis;
return; // we are done;
【讨论】: