This is an example from the official concurrency tutorial.
http://download.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
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.
http://download.oracle.com/javase/tutorial/essential/concurrency/newlocks.html
分享到:
相关推荐
Approaches to Probabilistic Model Learning for Mobile Manipulation Robots,本书是概率模型应用于机器人方面不可多得的资料,作者Jürgen Sturm ,Springer-Verlag Berlin Heidelberg (2013)
翻译研究的入门教材,适合本科生研究生和翻译爱好者。原版书籍,观点新颖,实用性强。
《Equilibrium Approaches to Modern Deep Learning》这篇论文提出了一个全新的思路,即深度均衡(DEQ)模型,它挑战了传统基于层层堆叠的深度学习范式。 DEQ模型的核心思想是将输出视为动态系统的固定点,从而实现...
《Digital Approaches to Text Reuse in the Early Chinese Corpus》一文由Donald Sturgeon撰写,旨在探讨利用数字化技术来识别早期中国文献中不同文本之间的相似性。文章通过对古典文献《墨子》的具体分析,展示了...
Uses the open source tf-seq2seq framework to train a char2char model on the E2E NLG Challenge data. No delexicalization, lowercasing or even tokenization Input semantics = sequence of characters Human...
本书介绍了人工神经网络(ANN)领域的一些贡献。所讨论的主题是多学科性质,并且密切相关,其最终目的是从动态现实信号交换和不变机器表示中识别特征,可以利用它们来提高终端用户的生活质量。 ...
"An Evaluation of Statistical Approaches to Text Categorization." _Carnegie Mellon University, School of Computer Science_. 通过上述分析可以看出,《统计方法在文本分类中的评估》这篇文章不仅对当时流行...
关于偏微分方程的代数方法的电子书,最新版本,经典著作,适合工学理学等领域
当前基因调控网络建模的方法与进展 在生物学领域,基因调控网络(Gene Regulatory Network, GRN)的研究一直是热点话题,其复杂性和动态性为科学家们提供了深入理解生物体内部运作机制的关键线索。...
根据给定文件的信息,本文将对图形理论在图像分割中的应用进行一次全面的综述,主要聚焦于图形理论方法在解决计算机视觉基本问题——图像分割中的应用与优势。 ### 图像分割及其挑战 图像分割是计算机视觉领域的一...
Social network approaches provide a set of theories and methods with which to articulate and investigate, with greater precision and rigor, the wide variety of relational perspectives implied by ...
Approaches to the Natural World Based on Math-Physical Three Principles,叶鹰,,Synthesized modern mathematics and physics, the author proposes the approaches to the natural world with math-physical ...
Uncertainties signal a need for new approaches to mobility.pdf
【co-data的数据科学杂志文章】Approaches to Open Data for Science in Spain
这篇名为"Novel Approaches to the Design of Phased Array Antennas"的论文深入探讨了这一领域的最新设计理念和方法。相控阵天线的工作原理是通过控制各个单元天线的相位来实现波束的方向性,从而实现对电磁波的...