x++
我从一个学生那里得到了这段代码,由于涉及and的竞争条件,它不能正常工作x--
。他添加synchronized
了run()
试图摆脱这个错误的方法,但显然这只排除了线程进入同一个run()
对象(这从一开始就不是问题),但不会阻止独立对象更新相同的静态变量同时。x
public class DataRace implements Runnable {
static volatile int x;
public synchronized void run() {
for (int i = 0; i < 10000; i++) {
x++;
x--;
}
}
public static void main(String[] args) throws Exception {
Thread [] threads = new Thread[100];
for (int i = 0; i < threads.length; i++)
threads[i] = new Thread(new DataRace());
for (int i = 0; i < threads.length; i++)
threads[i].start();
for (int i = 0; i < threads.length; i++)
threads[i].join();
System.out.println(x); // x not always 0!
}
}
由于我们无法同步 on x
(因为它是原始的),我能想到的最佳解决方案是创建一个新的静态对象,例如并将andstatic String lock = "";
包含在一个块中,锁定 on 。但这似乎真的很尴尬。有没有更好的办法?x++
x--
synchronized
lock