wait() 和 yield() 的区别 [英] difference between wait() and yield()

查看:57
本文介绍了wait() 和 yield() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我对wait() 和yield() 方法的理解是当线程不执行任何任务时调用yield() 并让CPU 执行其他线程.wait() 在某个线程被搁置时使用,通常用于同步的概念.但是,我无法理解它们的功能差异,我不确定我所理解的是对还是错.有人可以解释一下它们之间的区别吗(除了它们所在的包装).

解决方案

他们不是都在做同样的任务——等待其他线程可以执行吗?

甚至没有关闭,因为 yield() 不会等待任何事情.

每个线程都可以处于多种不同状态中的一种:Running 意味着该线程实际上在 CPU 上运行,Runnable 意味着没有任何东西阻止该线程除了运行之外,可能还有 CPU 可以运行.所有其他状态都可以归入一个称为阻塞的类别.阻塞的线程是在它变得可运行之前等待某些事情发生的线程.

操作系统定期抢占正在运行的线程:操作系统每隔一段时间(在大多数操作系统上为每秒 10 次到每秒 100 次)标记每个正在运行的线程并说,轮到你了,转到运行队列的后面'(即,将状态从运行更改为可运行).然后它让运行队列头部的任何线程使用该 CPU(即再次运行).

当您的程序调用 Thread.yield() 时,它对操作系统说:我还有工作要做,但它可能不如其他线程的工作重要正在做.现在请把我送到运行队列的后面."如果线程有可用的 CPU 可以运行,那么它实际上将继续运行(即,yield() 调用将立即返回).

另一方面,当您的程序调用 foobar.wait() 时,它对操作系统说:阻止我,直到其他线程调用 foobar.notify()>.

Yielding 最初是在非抢占式操作系统和非抢占式线程库中实现的.在只有一个 CPU 的计算机上,多个线程运行的唯一方式是线程显式地相互让步.

让步对于忙碌的等待也很有用.这就是线程通过坐在一个紧密的循环中,一遍又一遍地测试相同条件来等待某事发生的地方.如果条件依赖于其他线程来做一些工作,等待的线程会在每次循环中 yield() 以便让其他线程完成它的工作.

既然我们拥有为我们提供更高级别同步对象的抢占和多处理器系统和库,那么应用程序基本上没有理由再需要调用 yield().>

So far what I have understood about wait() and yield () methods is that yield() is called when the thread is not carrying out any task and lets the CPU execute some other thread. wait() is used when some thread is put on hold and usually used in the concept of synchronization. However, I fail to understand the difference in their functionality and i'm not sure if what I have understood is right or wrong. Can someone please explain the difference between them(apart from the package they are present in).

解决方案

aren't they both doing the same task - waiting so that other threads can execute?

Not even close, because yield() does not wait for anything.

Every thread can be in one of a number of different states: Running means that the thread is actually running on a CPU, Runnable means that nothing is preventing the thread from running except, maybe the availability of a CPU for it to run on. All of the other states can be lumped into a category called blocked. A blocked thread is a thread that is waiting for something to happen before it can become runnable.

The operating system preempts running threads on a regular basis: Every so often (between 10 times per second and 100 times per second on most operating systems) the OS tags each running thread and says, "your turn is up, go to the back of the run queue' (i.e., change state from running to runnable). Then it lets whatever thread is at the head of the run queue use that CPU (i.e., become running again).

When your program calls Thread.yield(), it's saying to the operating system, "I still have work to do, but it might not be as important as the work that some other thread is doing. Please send me to the back of the run queue right now." If there is an available CPU for the thread to run on though, then it effectively will just keep running (i.e., the yield() call will immediately return).

When your program calls foobar.wait() on the other hand, it's saying to the operating system, "Block me until some other thread calls foobar.notify().

Yielding was first implemented on non-preemptive operating systems and, in non-preemptive threading libraries. On a computer with only one CPU, the only way that more than one thread ever got to run was when the threads explicitly yielded to one another.

Yielding also was useful for busy waiting. That's where a thread waits for something to happen by sitting in a tight loop, testing the same condition over and over again. If the condition depended on some other thread to do some work, the waiting thread would yield() each time around the loop in order to let the other thread do its work.

Now that we have preemption and multiprocessor systems and libraries that provide us with higher-level synchronization objects, there is basically no reason why an application programs would need to call yield() anymore.

这篇关于wait() 和 yield() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆