- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下使用 Java 的 Semaphore 类制作的并发代码进入了死锁,甚至很艰难,根据控制台输出,许可证正在释放。
package ThreadTraining;
import java.util.concurrent.Semaphore;
public class ThreadTraining {
public static class Value {
private static int value = 0;
private static final Semaphore SEMAPHORE = new Semaphore(1);
public static synchronized void acquire() throws InterruptedException {
SEMAPHORE.acquire();
System.out.println("A thread has aquired a permit!");
}
public static synchronized void release() {
SEMAPHORE.release();
}
public static int get() {
return value;
}
public static void add() {
value++;
}
public static void subtract() {
value--;
}
}
public static class Adder extends Thread {
public Adder(String name) {
this.setName(name);
}
@Override
public void run() {
System.out.println(this.getName() + " has been created.");
boolean keepRunning = true;
while (keepRunning) {
try {
Value.acquire();
System.out.print(this.getName() + " has aquired Value's permit. --- ");
if (Value.get() > 99) {
System.out.print(this.getName() + " has finished it's job. --- ");
keepRunning = false;
} else {
System.out.print(this.getName() + " has modified value from " + Value.get() + " to ");
Value.add();
System.out.println(Value.get() + ".");
}
} catch (InterruptedException ie) {
System.err.println("This thread was interrupted.");
} finally {
System.out.println(this.getName() + " is releasing Value's permit.");
Value.release();
}
}
}
}
public static void main(String[] args) {
Thread threads[] = new Thread[3];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Adder("[Adder]Thread #" + i);
}
for (Thread t : threads) {
t.start();
}
}
}
代码的控制台输出:(这是一个“幸运的”运行,它通常只打印到指定的点)
[Adder]Thread #0 has been created.
[Adder]Thread #1 has been created.
[Adder]Thread #2 has been created.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 0 to 1.
[Adder]Thread #0 is releasing Value's permit. /*NOTE: It usually prints only up to this line, hanging after the first permit-release.*/
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 1 to 2.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 2 to 3.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 3 to 4.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 4 to 5.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 5 to 6.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 6 to 7.
[Adder]Thread #0 is releasing Value's permit.
A thread has aquired a permit!
[Adder]Thread #0 has aquired Value's permit. --- [Adder]Thread #0 has modified value from 7 to 8.
[Adder]Thread #0 is releasing Value's permit.
背后的原因是什么?并且,如果可能的话,如何修复它?
附加信息:
这个问题是my previous concurrency question的“延续” .
新代码非常基于this semaphore tutorial .
最佳答案
问题是你自己方法的同步问题。 Value.acquire
和 Value.release
都是同步的,因此进入您的 acquire
方法的线程将阻止另一个线程调用 release
,因为 release
调用将等待 Value
类的监视器被释放,而 acquire
中的监视器将等待要获取的内部信号量。从您的方法中删除 synchronized
-关键字,您将摆脱死锁问题。相反,您可能打算同步您的 get
-、add
- 和 subtract
- 方法。
关于java - 即使正在释放许可,Semaphore 类是否会进入死锁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10640362/
我有类似下面的代码: ... 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
我是一名优秀的程序员,十分优秀!