Java 多线程的sleep方法和yield方法有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9700871/
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
what is difference between sleep method and yield method of multi threading?
提问by Ajay Takur
As currently executing thread while it encounters the call [sleep][1]();
then thread moves immediately into sleeping state
where as for [yield][2]();
thread moves into runnable state/ready state
作为当前正在执行的线程,当它遇到调用时,[sleep][1]();
线程立即进入睡眠状态,而[yield][2]();
线程进入可运行状态/就绪状态
采纳答案by Michael Borgwardt
sleep()
causes the thread to definitely stop executing for a given amount of time; if no other thread or process needs to be run, the CPU will be idle (and probably enter a power saving mode).
sleep()
导致线程在给定的时间内绝对停止执行;如果没有其他线程或进程需要运行,CPU 将处于空闲状态(并且可能进入省电模式)。
yield()
basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should. Otherwise, the current thread will continue to run.
yield()
基本上意味着线程没有做任何特别重要的事情,如果需要运行任何其他线程或进程,它们应该运行。否则,当前线程将继续运行。
回答by Anantha Krishnan
We can prevent a thread from execution by using any of the 3 methods of Thread class:
我们可以通过使用 Thread 类的 3 个方法中的任何一个来阻止线程执行:
yield()
join()
sleep()
yield()
join()
sleep()
yield()
method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The yielded thread when it will get the chance for execution is decided by the thread scheduler whose behavior is vendor dependent.join()
If any executing thread t1 callsjoin()
on t2 i.e;t2.join()
immediately t1 will enter into waiting state until t2 completes its execution.sleep()
Based on our requirement we can make a thread to be in sleeping state for a specified period of time (hope not much explanation required for our favorite method).
yield()
方法会暂时暂停当前正在执行的线程,以便让具有相同优先级的剩余等待线程有机会执行。如果没有等待线程或所有等待线程的优先级较低,则同一线程将继续执行。获得执行机会的线程由线程调度程序决定,线程调度程序的行为取决于供应商。join()
如果任何正在执行的线程 t1 调用join()
t2,即;t2.join()
t1 将立即进入等待状态,直到 t2 完成其执行。sleep()
根据我们的需求,我们可以让一个线程在指定的时间段内处于睡眠状态(希望我们最喜欢的方法不需要太多解释)。
回答by giorashc
Sleep causes thread to suspend itself for x milliseconds while yield suspends the thread and immediately moves it to the ready queue (the queue which the CPU uses to run threads).
Sleep 使线程自己挂起 x 毫秒,而 yield 挂起线程并立即将其移动到就绪队列(CPU 用于运行线程的队列)。
回答by Kazekage Gaara
Sleep()causes the currently executing thread to sleep (temporarily cease execution).
Sleep()使当前正在执行的线程休眠(暂时停止执行)。
Yield()causes the currently executing thread object to temporarily pause and allow other threads to execute.
Yield()使当前正在执行的线程对象暂时暂停并允许其他线程执行。
Read thisfor a good explanation of the topic.
阅读本文以获得对该主题的良好解释。
回答by Sai Sunder
sleep()causes the thread to definitely stop executing for a given amount of time; if no other thread or process needs to be run, the CPU will be idle (and probably enter a power saving mode). yield()basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should. Otherwise, the current thread will continue to run.
sleep() 导致线程在给定的时间内绝对停止执行;如果没有其他线程或进程需要运行,CPU 将处于空闲状态(并且可能进入省电模式)。yield() 基本上意味着线程没有做任何特别重要的事情,如果需要运行任何其他线程或进程,它们应该运行。否则,当前线程将继续运行。
回答by Preetham
Yield : will make thread to wait for the currently executing thread and the thread which has called yield() will attaches itself at the end of the thread execution. The thread which call yield() will be in Blocked state till its turn.
Yield :将使线程等待当前正在执行的线程,并且调用了 yield() 的线程将在线程执行结束时附加自身。调用 yield() 的线程将处于阻塞状态,直到轮到它为止。
Sleep : will cause the thread to sleep in sleep mode for span of time mentioned in arguments.
Sleep :将导致线程在参数中提到的时间跨度下以睡眠模式睡眠。
Join : t1 and t2 are two threads , t2.join() is called then t1 enters into wait state until t2 completes execution. Then t1 will into runnable state then our specialist JVM thread scheduler will pick t1 based on criteria's.
join : t1 和 t2 是两个线程,调用 t2.join() 然后 t1 进入等待状态,直到 t2 完成执行。然后 t1 将进入可运行状态,然后我们的专业 JVM 线程调度程序将根据标准选择 t1。
回答by Prashant_M
Yield: It is a hint (not guaranteed) to the scheduler that you have done enough and that some other thread of same priority might run and use the CPU.
Yield:这是对调度程序的一个提示(不保证),您已经完成了足够的工作,并且其他一些具有相同优先级的线程可能会运行并使用 CPU。
Thread.sleep();
Sleep: It blocks the execution of that particular thread for a given time.
Sleep:它在给定时间内阻止该特定线程的执行。
TimeUnit.MILLISECONDS.sleep(1000);
回答by Soudipta Dutta
One way to request the current thread to relinquish CPU so that other threads can get a chance to execute is to use yield
in Java.
请求当前线程放弃 CPU 以便其他线程有机会执行的一种方法是yield
在 Java 中使用。
yield
is a static method.
It doesn't say which other thread will get the CPU.
It is possible for the same thread to get back the CPU and start its execution again.
yield
是一个静态方法。它没有说明哪个其他线程将获得 CPU。同一个线程有可能取回 CPU 并再次开始执行。
public class Solution9 {
public static void main(String[] args) {
yclass yy = new yclass ();
Thread t1= new Thread(yy);
t1.start();
for (int i = 0; i <3; i++) {
Thread.yield();
System.out.println("during yield control => " + Thread.currentThread().getName());
}
}
}
class yclass implements Runnable{
@Override
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("control => " + Thread.currentThread().getName());
}
}
}
回答by Pankaj Goyal
yield(): yield method is used to pause the execution of currently running process so that other waiting thread with the same priority will get CPU to execute.Threads with lower priority will not be executed on yield. if there is no waiting thread then this thread will start its execution.
yield():yield 方法用于暂停当前正在运行的进程的执行,以便其他具有相同优先级的等待线程获得 CPU 执行。低优先级的线程不会在 yield 时执行。如果没有等待线程,则该线程将开始执行。
join(): join method stops currently executing thread and wait for another to complete on which in calls the join method after that it will resume its own execution.
join():join 方法停止当前正在执行的线程并等待另一个线程完成,然后调用 join 方法,然后它将恢复自己的执行。
For detailed explanation, see this link.
有关详细说明,请参阅此链接。
回答by Sumit Singh
Yield(): method will stop the currently executing thread and give a chance to another thread of same priority which are waiting in queue. If thier is no thread then current thread will continue to execute. CPU will never be in ideal state.
Yield(): 方法将停止当前正在执行的线程,并给另一个在队列中等待的相同优先级的线程一个机会。如果没有线程,则当前线程将继续执行。CPU 永远不会处于理想状态。
Sleep(): method will stop the thread for particular time (time will be given in milisecond). If this is single thread which is running then CPU will be in ideal stateat that period of time.
Sleep(): 方法将在特定时间停止线程(时间将以毫秒为单位)。如果这是正在运行的单线程,那么CPU在该时间段将处于理想状态。
Both are static menthod.
两者都是静态方法。