如果在synchronized方法中调用,thread.yield()是否会丢失对象的锁定? [英] Does thread.yield() lose the lock on object if called inside a synchronized method?

查看:120
本文介绍了如果在synchronized方法中调用,thread.yield()是否会丢失对象的锁定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解 Thread.currentThread()。yield()是对线程调度程序的通知,它可以将cpu周期分配给具有相同优先级的其他线程(如果有的话)存在。
我的问题是:如果当前线程已锁定某个对象并调用 yield(),它会立即丢失该锁定吗?并且当线程调度程序发现没有这样的线程来分配cpu周期时,那么调用 yield()的线程将再次在争夺对象的锁定它已经丢失了吗?

I understand that Thread.currentThread().yield() is a notification to thread scheduler that it may assign cpu cycle to some other thread of same priority if any such is present. My question is: If current thread has got lock on some object and calls yield(), will it loses that lock right away? And when thread scheduler finds out there is no such thread to assign cpu cycle, then the thread which has called yield() will again be in fight to get lock on the object which it has lost earlier??

我在javadoc和论坛中找不到它[http://www.coderanch.com/t/226223/java-programmer- SCJP / certification / does-sleep-yield-release-lock]有50-50个答案。

I couldn't find it in javadoc and forums [http://www.coderanch.com/t/226223/java-programmer-SCJP/certification/does-sleep-yield-release-lock] have 50-50 answers.

我认为 yield()(比如说thread1)应该释放锁定,因为如果某个相同优先级的线程(比如说thread2)想要对同一个对象进行操作,那么当线程调度程序最终将cup分配给thread2时它就有机会。

I think yield() (lets say thread1) should release lock because if some thread (lets say thread2) of same priority wants to operate on same object, then it can have chance when thread scheduler eventually assign cup to thread2.

推荐答案

不。 Thread.yield() Object.wait()不同。它只是放弃了允许线程切换的控制。它对程序的并发性没有影响。

No. Thread.yield() is not like Object.wait(). It just gives up control to allow a thread switch. It will have no effect on the concurrency of your program.

无法保证调度程序在收益后运行哪个线程。

There is no guarantee which thread the scheduler will run after a yield.

这篇关于如果在synchronized方法中调用,thread.yield()是否会丢失对象的锁定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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