在这种情况下会发生死锁吗? [英] Does a Deadlock Occur in This Case?

查看:182
本文介绍了在这种情况下会发生死锁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是否正确地说在以下情况下应该发生死锁:

Am I right in saying that a deadlock is supposed to happen in the following case:

对象 P 调用对象的synch方法 A

调用对象 B 的同步方法,

调用对象 A的同步方法

Object P calls a synch method of object A,
that calls a synch method of object B,
that calls a synch method of object A.

很抱歉,如果它看起来很愚蠢,很可能就是这样。但那是为什么我在问。谢谢!

Sorry if it looks stupid of me, most probably it is. But that's why I'm asking. Thanks!

推荐答案

根据您提供的信息 - 不,不会发生死锁:

By the information you give - no, a deadlock can't occur:

首先,你没有提到多个线程。单个线程不会导致死锁。但是我们假设您有多个线程。

First, you don't mention multiple threads. A single thread can't cause a deadlock. But let's assume you have multiple threads.

因此,如果任何其他对象,来自不同的线程以相反的顺序调用其中一些方法,那么可能发生死锁。

So, if any other object, from different thread invokes some of these methods in reverse order, then a deadlock can occur.

情况说明如下: Thread-1 获取所需的锁定输入 methodA ,然后尝试输入 methodB 。如果在同一时刻另一个线程 - Thread-2 调用 methodB 并获取它的锁定,然后尝试进入 methodA ,但 Thread-1 已经有锁,所以 Thread-2 等待。但是, Thread-1 无法输入 methodB ,因为 Thread-2 有锁。他们永远等待(死锁)。

The explanation of the situation is as follows: Thread-1 obtains the lock required to enter methodA, and then tries to enter methodB. If at the same moment another thread - Thread-2 invokes methodB and obtains the lock for it, then tries to enter methodA, but Thread-1 already has the lock, so Thread-2 waits. However, Thread-1 can't enter methodB because Thread-2 has the lock. And they wait forever (deadlock).

这篇关于在这种情况下会发生死锁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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