简体   繁体   English

只有一个同步点的任何死锁机会?

[英]Any chance of deadlock with only one sync point?

I have two running threads calling few methods (5 or 6) where I specified synchronized block inside and use only one object to lock it. 我有两个正在运行的线程调用几个方法(5或6),我在其中指定了synchronized块,并且只使用一个对象来锁定它。 Is there any chance of deadlock with having only one sync point? 只有一个同步点是否有任何死锁的可能性? So far, I haven't seen such a situation, but still. 到目前为止,我还没有看到过这样的情况。 Thanks for help. 感谢帮助。

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. 死锁描述了两个或多个线程永远被阻塞,等待彼此的情况。

If your methods get called from each other there might be a situation. 如果你的方法相互调用,可能会出现这种情况。 Find more detail with example in documentation . 通过文档中的示例查找更多细节。

If you are only using synchronized calls (ie no join, wait, notify, etc.) then the only way you can "deadlock" (using the term broadly here) is if one of the threads fails to exit the synchronised block (eg executes an infinite loop, doesn't return from a call to request some resource, etc.). 如果您使用synchronized调用(即没有连接,等待,通知等),那么您可以“死锁”(在此广泛使用术语)的唯一方法是,其中一个线程无法退出同步块(例如执行无限循环,不会从调用返回请求某些资源等)。 So, yes, it's possible. 所以,是的,这是可能的。

If you just have one lock for locking shared resources deadlock cannot occur. 如果您只有一个锁用于锁定共享资源,则不会发生死锁。 You can check if the Coffman's conditions are met to identify potential deadlock. 您可以检查是否满足Coffman的条件以识别潜在的死锁。

Without seeing your code it is difficult to say. 没有看到你的代码,很难说。 But as you have described it you are most likely fine. 但正如你所描述的那样,你最有可能。 This link talks about ways of avoiding deadlock. 此链接讨论了避免死锁的方法。 For example take the following quote from the article, it says that one way to avoid deadlocks is to check for a "...nested synchronized block or calling one synchronized method from other or trying to get lock on different object". 例如,从文章中引用以下引用,它说避免死锁的一种方法是检查“...嵌套的同步块或从其他方法调用一个同步方法或试图锁定不同的对象”。

Another thing you should be aware of is a live lock . 您应该注意的另一件事是实时锁定 This occurs when one threads action is in response to another threads. 当一个线程动作响应另一个线程时,会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 只需点击一下,是否有机会获得双击按钮? - Is there any chance to get a button with double clicked after disabling it with one click? 在JAVA中的任何时候检查四个变量中是否只有一个变量设置的条件 - if condition to check if only one variable is set of the four variables at any point in JAVA javafuse有什么机会可以工作吗? - Any chance javafuse can work? 此代码是否有可能打印为空? - Is there any chance of this code printing null? 我们如何确保在任何时间仅调用类的静态方法和非静态方法之一? - How can we make sure that at any point of time only one of a static method & a non-static method of a class gets called? 一锁Java死锁 - Java deadlock with one lock 是否有机会将对象自动转换为Integer? - Any chance of Object auto casting to Integer? 是否有机会连接到主机上的在线数据库? - Is there any chance for connecting to online database in a host machine? 此Java代码是否有可能出现ClassCastException / IllegalStateException? - Is there any chance of ClassCastException/IllegalStateException with this java code? 仅与 Galera MYSQL 集群发生死锁 - Deadlock only with Galera MYSQL cluster
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM