- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在java教程中,我看到了这个简单的死锁示例,并且由于某种原因无法弄清楚它为什么被阻止
public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format("%s: %s"
+ " has bowed to me!%n",
this.name, bower.getName());
System.out.println("Nice try " + this.name);
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s"
+ " has bowed back to me!%n",
this.name, bower.getName());
}
}
public static void main(String[] args) {
final Friend alphonse =
new Friend("Alphonse");
final Friend gaston =
new Friend("Gaston");
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}
}
让我们看一些真实的例子
run:
Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed to me!
Nice try Gaston
Nice try Alphonse
因此,Alphonse 和 Gaston 现在都运行“bow()”,每个人都在“bow()”末尾尝试互相调用“bowBack()”,“bowBack()”不是静态的,因此 Alphonse 和 Gaston 已经分离了这个方法的实例,并且之前没有调用“bowBack()”,因此它不应该被阻止。那么为什么会出现死锁。
在教程中 http://docs.oracle.com/javase/tutorial/essential/concurrency/deadlock.html
解释为
When Deadlock runs, it's extremely likely that both threads will block when they
attempt to invoke bowBack. Neither block will ever end, because each thread is
waiting for the other to exit bow.
但是为什么,要运行“bowBack()”,它应该首先等待“bow()”结束,如果它们不互相干扰的话
Alphonse.bow()--->尝试调用-->Gaston.bowBack()
Gaston.bow()--->尝试调用-->Alphonse.bowBack()
最佳答案
but why, to run "bowBack()" it should first wait the end of the "bow()", if they don't interfere with each other
当调用 bowBack()
时 - 正在运行的线程尝试获取的锁是它尝试调用其方法的 Object
上的锁。
这里 - Alphonse 是一个对象,Gaston 是第二个对象。
Alphonse.bow()--->尝试调用-->Gaston.bowBack()
在上面,第一个调用锁定了 Alphonse,并在该锁定仍保持的同时尝试获取 Gaston 的锁定。
因此,在线程 1 中:锁定 (Alp) 并尝试锁定(Gaston),而不释放锁定(Apl)
Gaston.bow()--->尝试调用-->Alphonse.bowBack()
在这里,第一个 Bow() 获取 Gaston 对象上的锁,然后尝试获取 Alphonse 上的锁。
因此,在线程 2 中:锁定 (Gaston) 并尝试锁定(Apl),而不释放锁定(Gaston)
如果线程 1 和线程 2 中的第一步都已完成,那么您可以算出第二步永远无法完成 - 因此出现死锁
关于java - 多线程死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24580686/
我有类似下面的代码: ... id: myComponent signal updateState() property variant modelList: [] Repeater { mo
我正在处理一些我无法展示的私有(private)代码,但我已经制作了一些示例代码来描述我的问题: 主.c: #include #include #include #include typede
这个问题在这里已经有了答案: 关闭10 年前。 Possible Duplicate: what are the differences in die() and exit() in PHP? 我想
在编写 Perl 模块时,在模块内部使用 croak/die 是一个好习惯吗? 毕竟,如果调用者不使用 eval block ,模块可能会使调用它的程序崩溃。 在这些情况下,最佳做法是什么? 最佳答案
我有一些搜索线程正在存储结果。我知道当线程启动时,JVM native 代码会代理在操作系统上创建新 native 线程的请求。这需要 JVM 之外的一些内存。当线程终止并且我保留对它的引用并将其用作
我刚刚花了很多时间调试一个我追溯到 wantarray() 的问题。 .我已将其提炼为这个测试用例。 (忽略 $! 在这种情况下不会有任何有用信息的事实)。我想知道为什么wantarray在第二个示例
我看到一些代码是这样做的: if(something){ echo 'exit from program'; die; } ...more code 和其他只使用 die 的人: if
我正在尝试将此表格用于: 如果任何 $_POST 变量等于任何其他 $_POST 变量抛出错误。 如果只有几个,那不是问题,但我有大约 20 个左右所以如果我想这样做,我将不得不像这样 但这
每次我运行: hadoop dfsadmin -report 我得到以下输出: Configured Capacity: 0 (0 KB) Present Capacity: 0 (0 KB) DFS
我是一名优秀的程序员,十分优秀!