- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法理解为什么下面的程序在奇偶场景下不起作用。
我得到的输出是在奇数线程1
如果我理解的话程序执行-
转到 t1 线程。条件不满足。调用notifyAll,它不会被注意到。转到t2线程,该线程打印奇数增量并调用wait()。在这种情况下T2应该开始运行吧?
public class OddEvenTest {
static int max = 20;
static int count = 1;
static Object lock = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
while(count % 2 == 0 && count < max) {
System.out.println("In even Thread " +count);
count++;
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
lock.notifyAll();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock) {
while(count % 2 != 0 && count < max) {
System.out.println("In Odd Thread " +count);
count++;
try {
lock.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
lock.notifyAll();
}
}
});
t1.start();
t2.start();
}
}
最佳答案
您不想在锁
上同步的循环中盲目地wait()
。相反,请检查您等待的先决条件,例如
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
while (count < max) {
synchronized (lock) {
if (count % 2 == 0) {
System.out.println("In Even Thread " + count);
count++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
while (count < max) {
synchronized (lock) {
if (count % 2 != 0) {
System.out.println("In Odd Thread " + count);
count++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
});
在 Java 8+ 中,您可以使用 lambda 来简化匿名类,例如
Thread t1 = new Thread(() -> {
while (count < max) {
synchronized (lock) {
if (count % 2 == 0) {
System.out.println("In Even Thread " + count);
count++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
Thread t2 = new Thread(() -> {
while (count < max) {
synchronized (lock) {
if (count % 2 != 0) {
System.out.println("In Odd Thread " + count);
count++;
lock.notifyAll();
} else {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
});
关于java - 使用两个线程顺序打印奇偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57335165/
http://opencv-code.com/quick-tips/implementation-of-guo-hall-thinning-algorithm/ 在看郭霍尔算法,但我不明白它说的那部分
我知道这个方法检查整数是否为偶数,但是具体如何检查呢?我理解斐波那契或阶乘等例子的递归是如何工作的,但不理解这个。我认为由于语法原因我不明白。 // Assume n >= 0 public stat
我被困在一个作业上,需要程序接受多个数字,然后如果是奇数则输出"is",如果是偶数则输出“否”,并且不知道如何让程序接受超过 1 个 int,然后输出正确的 println。这是我到目前为止的代码。
这个测试行得通吗?: if (testInt/2).ofType(Integer){ //to-do if even } 我假设它会 iff 编译器在 ofType() 之前解析 testIn
我正在尝试更好地排列图像,而不仅仅是 1 列中的图像。示例见附件,每篇文章的图片可以在左右。 这是我的代码。HTML: Content 1
DAY16共3题: 奇♂妙拆分(简单数学) 区区区间间间(单调栈) 小AA的数列(位运算dp) 🎈 作者:Eriktse 🎈 简介:19
我是一名优秀的程序员,十分优秀!