我对下面的代码有疑问。在这种情况下,我预计静态变量'a'和'b'都会出现竞争条件,并且期望两个变量的输出都小于1000.但是,当我执行下面的代码时,没有观察到预期的行为。显示,即b的输出总是1000.但是,当我取消注释用箭头标记的行并执行下面的代码时,观察到两个变量的竞争条件,即变量'a'和'b'的输出是少于1000.需要帮助。请原谅我,如果我错过了或忽略了线程的任何基本或基本概念,考虑到我仍然是java线程的新手!
public class SampleRace {
public static void main(String[] args) {
// TODO Auto-generated method stub
SampleRace1 a = new SampleRace1();
SampleRace1 b = new SampleRace1();
Thread t1 = new Thread(a);
Thread t2 = new Thread(b);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(SamapleLoop.a); // <---------------------
System.out.println(SampleRace1.b);
}
}
class SamapleLoop {
public static int a = 0;
public static void loop() {
a++;
}
}
class SampleRace1 implements Runnable {
public static int b = 0;
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 1; i <= 500; i++) {
//SamapleLoop.loop(); // <------------------------
b++;
}
}
}
答案 0 :(得分:1)
尝试将500增加到500.000.000,或在循环内执行更复杂的操作。在线程a
开始之前,线程b
有可能完成运行 - 因此它们最终按顺序运行,而不是并行运行。
你也可以每隔几个周期调用Thread.yield
放弃你的时间片,让另一个线程运行。
答案 1 :(得分:0)
如果你想我可以提出更合适的竞争条件,你可以继续
import java.util.concurrent.atomic.AtomicInteger;
/*
* author: Atom Karinca
*/
/*
* Run this program multiple times to see the output.
* How does the result vary? Why does that happen?
*
*/
class Racer extends Thread {
private static final int NUM_ITERS = 1000000;
public void run() {
for(int j = 0; j < NUM_ITERS; j++) {
RaceCondition.sum();
}
}
}
public class RaceCondition {
public static long sum = 0;
// What happens when you make this method "synchronized"?
// Do you still see the bug?
//synchronized
public static void sum() {
sum = sum + 1;
}
public static void main(String[] args) throws InterruptedException {
Racer leftAdder = new Racer();
Racer rightAdder = new Racer();
leftAdder.start();
rightAdder.start();
// wait for the threads to finish
leftAdder.join();
rightAdder.join();
System.out.println("Sum: " + sum);
}
}