Java Thread.sleep() 如何在内部工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24358989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How Thread.sleep() works internally
提问by user3766199
Let's say I have a thread T and it is holding one resource R. If I call Thread.sleep() on the current thread i.e T, will it release the resource R (to let other threads use it) before going to sleep or not? Or it will hold that resource and again when it will awake it will use the resource R and after finishing the work will it release it?
假设我有一个线程 T 并且它持有一个资源 R。如果我在当前线程(即 T)上调用 Thread.sleep(),它是否会在进入睡眠之前释放资源 R(让其他线程使用它) ? 或者它会持有那个资源,当它醒来时它会再次使用资源 R 并在完成工作后释放它?
回答by TheLostMind
The thread which is going to sleep will hold the lock(not release resource)
while it sleeps. A sleeping thread will not even be scheduled for the time it sleeps (or until it is interrrupted and then it wakes up)
将要休眠的线程将(not release resource)
在它休眠时持有锁。睡眠线程甚至不会被安排在它睡眠的时间(或者直到它被中断然后唤醒)
回答by Sergey Alaev
If your resource R is java monitor, then there are only two ways to release it:
如果你的资源R是java monitor,那么释放的方法只有两种:
- exit
synchronized
block - call
wait
on owned monitor
- 退出
synchronized
块 - 调用
wait
自有监视器
回答by rachana
From this Javamex article:
The Thread.sleep()method effectively "pauses" the current thread for a given period of time. We used it in our very first threading exampleto make threads display a message periodically, sleeping between messages. From the outset, it's important to be aware of the following:
it is always the currentthread that is put to sleep;
the thread might not sleepfor the required time (or even at all);
the sleep duration will be subject to some system-specific granularity, typically 1ms;
while sleeping, the thread still owns synchronization locksit has acquired;
the sleep can be interrupted (sometimes useful for implementing a cancellation function); calling sleep() with certain values can have some subtle, global effects on the OS (see below), and vice versa, other threads and processes running on the system can have subtle effects on the observed sleep duration.
的Thread.sleep()方法有效方法“暂停”的当前线程的给定时间段。我们在第一个线程示例中使用它来使线程定期显示消息,并在消息之间休眠。从一开始,请务必注意以下事项:
始终是当前线程进入睡眠状态;
该线程可能睡不着了所需的时间(甚至全部);
睡眠持续时间将受到某些系统特定 粒度的影响,通常为 1 毫秒;
在休眠时,线程仍然拥有它已获得的同步锁;
睡眠可以被中断(有时对于实现取消功能很有用);使用某些值调用 sleep() 会对操作系统产生一些微妙的全局影响(见下文),反之亦然,系统上运行的其他线程和进程会对观察到的睡眠持续时间产生微妙的影响。
回答by SparkOn
Javadocsays - sleep()
: 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
Javadoc说 - sleep()
: 使当前正在执行的线程休眠(暂时停止执行)指定的毫秒数,取决于系统计时器和调度程序的精度和准确性
The Thread.sleep() method essentially interacts with the thread scheduler to put the current thread into a wait state for the required interval. The thread however does not lose ownership of any monitors.
Thread.sleep() 方法本质上与线程调度程序交互,以将当前线程置于所需间隔的等待状态。但是,该线程不会失去任何监视器的所有权。
In order to allow interruption, the implementation may not actually use the explicit sleep function that most OS's provide.
为了允许中断,实现可能实际上不使用大多数操作系统提供的显式睡眠功能。
If the current thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int)
, methods of Thread class
, then its interrupt status will be cleared and it will receive an InterruptedException.
如果当前线程阻塞的调用wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int)
,methods of Thread class
,则其中断状态将被清除,它还将收到一个InterruptedException。
回答by Nasruddin
First of all, Thread.sleep() is Blocking library method. Threads may block, or pause, for several reasons: waiting for I/O completion, waiting to acquire a lock, waiting to wake up from Thread.sleep, or waiting for the result of a computation in another thread. When a thread blocks, it is usually suspended and placed in one of the blocked thread states.
首先,Thread.sleep() 是 Blocking 库方法。线程可能由于多种原因而阻塞或暂停:等待 I/O 完成、等待获取锁、等待从 Thread.sleep 中唤醒或等待另一个线程中的计算结果。当线程阻塞时,它通常被挂起并置于阻塞线程状态之一。
So, when you call the sleep() method, Thread leaves the CPU and stops its execution for a period of time. During this time, it's not consuming CPU time, so the CPU can be executing other tasks.When Thread is sleeping and is interrupted, the method throws an InterruptedException exception immediately and doesn't wait until the sleeping time finishes.
因此,当您调用 sleep() 方法时,Thread 会离开 CPU 并停止执行一段时间。在这段时间里,它不消耗CPU时间,所以CPU可以执行其他任务。当Thread正在休眠并被中断时,该方法立即抛出InterruptedException异常,不会等到休眠时间结束。
The Java concurrency API has another method that makes a Thread object leave the CPU. It's the yield() method, which indicates to the JVM that the Thread object can leave the CPU for other tasks. The JVM does not guarantee that it will comply with this request. Normally, it's only used for debug purposes.
Java 并发 API 有另一种方法可以使 Thread 对象离开 CPU。它是 yield() 方法,它向 JVM 指示 Thread 对象可以离开 CPU 用于其他任务。JVM 不保证它会遵守此请求。通常,它仅用于调试目的。
One of the confusion with sleep() is that how it is different from wait() method of object class.
与 sleep() 的混淆之一是它与 object class 的 wait() 方法有何不同。
The major difference between wait and sleep is that wait() method release the acquired monitor when thread is waiting while Thread.sleep() method keeps the lock or monitor even if thread is waiting.
wait 和 sleep 之间的主要区别在于,wait() 方法在线程等待时释放获取的监视器,而 Thread.sleep() 方法即使线程正在等待也保持锁定或监视器。