Several approaches to solve deadlock

This is an example from the official concurrency tutorial.
[url]http://download.oracle.com/javase/tutorial/essential/concurrency/deadlock.html[/url]

The reason for the deadlock here is that there are two sync methods

public synchronized void bow(Friend bower) {
System.out.format("%s: %s has bowed to me!%n", this.name, bower
.getName());
bower.bowBack(this);
}

public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s has bowed back to me!%n", this.name,
bower.getName());
}

Say, you have two objects a and b, you call them from thread1 a.bow(b) and from thread2 b.bow(a). When a holds its own lock finished printing and trying to grab b's lock(by calling b.bowBack(a)), b will be holding its own lock, finished printing and trying to grab a's lock. Here comes the deadlock.

How to solve it?
A. Use the same lock for object a and b so that they can have exclusive access to bow(). Previous deadlock situation happens because there is no exclusive access to bow(). To do this, you can pass an explicit lock to both a and b(of course add one constructor for the lock), or you can simply use Class as the lock because both a and b share the same class object.

public void bow(Friend bower) {
synchronized (this.getClass()) {
System.out.format("[%s] %s: %s has bowed to me!%n", Thread.currentThread().getName(),this.name, bower.getName());
bower.bowBack(this);
}
}

public void bowBack(Friend bower) {
synchronized (this.getClass()) {
System.out.format("[%s] %s: %s has bowed back to me!%n", Thread.currentThread().getName(),
this.name, bower.getName());
}
}


B. You can maintain a universal order for getting locks so that no matter which thread access bow(), simultaneously they can only access different parts of it.

public void bow(Friend bower) {
int hashThis = System.identityHashCode(this);
int hashBower = System.identityHashCode(bower);

if (hashThis < hashBower) {
synchronized(this) {
synchronized(bower) {
System.out.printf("[%s]: this: %d, bower: %d\n", Thread.currentThread().getName(), hashThis, hashBower);
System.out.printf("[%s] %s bow to %s\n", Thread.currentThread().getName(), this.getName(), bower.getName());
bower.bowBack(this);
}

}
} else if (hashThis > hashBower) {
synchronized(bower) {
synchronized(this) {
System.out.printf("[%s]222 %s bow to %s\n", Thread.currentThread().getName(), this.getName(), bower.getName());
bower.bowBack(this);
}
}
} else {
synchronized (getClass()) {
System.out.printf("[%s]333 %s bow to %s", Thread.currentThread().getName(), this.getName(), bower.getName());
bower.bowBack(this);
}
}
}


C. Use explicit Lock, this is the approach that is provided by this official tutorial, which should achieve the best performance at practice. One of the advantages is that the tryLock() method can grab the lock or returns false if the lock is held by other threads.
check it out. [url]http://download.oracle.com/javase/tutorial/essential/concurrency/newlocks.html[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
深度学习方法在图像恢复领域中的调查结果显示,深度学习模型已成为目前最先进的图像恢复方法之一。深度学习模型的优势在于其学习能力和自动化特性。 首先,深度学习模型对于图像恢复任务的学习能力更强。深度学习模型通常基于深层神经网络,其具有多个层级的非线性变换,可以从大量数据中学习图像的特征和统计规律。这使得深度学习模型能够更准确地恢复损坏的图像,例如去噪、超分辨率恢复和图像修复等任务。 其次,深度学习模型的自动化特性使得图像恢复变得更加便捷。相较于传统的人工设计特征提取和手动调整参数的方法,深度学习模型可以通过端到端的训练,以更少的人工干预来完成图像恢复任务。这使得深度学习模型在实际应用中更易于使用和扩展。 在图像恢复领域,深度学习模型的发展也面临一些挑战。首先,深度学习模型的训练需要大量的标注数据,而获取高质量的标注数据是昂贵且耗时的。因此,如何有效地利用有限的标注数据进行深度学习模型的训练是一个重要的问题。 其次,深度学习模型的计算复杂度较高,特别是在处理大规模图像时。这导致深度学习模型在实际应用中可能会遇到计算资源和时间成本的限制。 综上所述,深度学习方法在图像恢复领域取得了显著的进展。然而,仍有一些挑战需要克服,包括标注数据的获取和深度学习模型的计算复杂度。随着技术的不断发展,深度学习模型在图像恢复领域的应用前景将更加广阔。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值