如果在Java 6中int递增/递减操作不是原子的,也就是说,它们被称为在几个步骤中执行(读取值,增量,写入等),我希望看到一段代码将会演示多个线程如何以一种完全破坏它的方式影响单个int变量。
例如,基本步骤包括但不包括所有这些: i ++〜=把i放到寄存器中;增量i(包括运算);把我写回记忆;
如果在此过程中两个或多个线程交错,则可能意味着在对i ++进行两次后续调用之后的值将仅增加一次。
你能在java中演示一段在多线程环境中模拟这种情况的代码吗?
答案 0 :(得分:3)
public class Test {
private static int counter;
public static void main(String[] args) throws InterruptedException {
Runnable r = new Runnable() {
public void run() {
for (int i = 0; i < 100000; i++) {
counter++;
}
}
};
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t2.start();
t1.join();
t2.join();
if (counter != 200000) {
System.out.println("Houston, we have a synchronization problem: counter should be 200000, but it is " + counter);
}
}
}
在我的机器上运行此程序
Houston, we have a synchronization problem: counter should be 200000, but it is 198459
答案 1 :(得分:1)
这是代码。对static
抱歉,只想保存几行代码,不会影响结果:
public class Test
{
public static int value;
public static void main(String[] args) throws InterruptedException
{
Runnable r = new Runnable() {
@Override
public void run() {
for(int i = 0; i < 50000; ++i)
++value;
}
};
List<Thread> threads = new ArrayList<Thread>();
for(int j = 0; j < 2; ++j)
threads.add(new Thread(r));
for (Thread thread : threads)
thread.start();
for (Thread thread : threads)
thread.join();
System.out.println(value);
}
}
此程序可以打印50000到100000之间的任何内容,但它实际上从未在我的机器上打印过100000。
现在使用int
和AtomicInteger
方法替换incrementAndGet()
。它将始终打印100000而不会对性能产生重大影响(它使用CPU CAS指令,无Java同步)。
答案 2 :(得分:1)
您需要为++
快速运行测试以进行多次迭代,并且可以在有时间出现问题之前运行完成。
public static void main(String... args) throws InterruptedException {
for (int nThreads = 1; nThreads <= 16; nThreads*=2)
doThreadSafeTest(nThreads);
}
private static void doThreadSafeTest(final int nThreads) throws InterruptedException {
final int count = 1000 * 1000 * 1000;
ExecutorService es = Executors.newFixedThreadPool(nThreads);
final int[] num = {0};
for (int i = 0; i < nThreads; i++)
es.submit(new Runnable() {
public void run() {
for (int j = 0; j < count; j += nThreads)
num[0]++;
}
});
es.shutdown();
es.awaitTermination(10, TimeUnit.SECONDS);
System.out.printf("With %,d threads should total %,d but was %,d%n", nThreads, count, num[0]);
}
打印
With 1 threads should total 1,000,000,000 but was 1,000,000,000
With 2 threads should total 1,000,000,000 but was 501,493,584
With 4 threads should total 1,000,000,000 but was 319,482,716
With 8 threads should total 1,000,000,000 but was 261,092,117
With 16 threads should total 1,000,000,000 but was 202,145,371
只有500K我在基本笔记本电脑上得到了以下内容。在更快的机器上,您可以在看到问题之前获得更高的迭代次数。
With 1 threads should total 500,000 but was 500,000
With 2 threads should total 500,000 but was 500,000
With 4 threads should total 500,000 but was 500,000
With 8 threads should total 500,000 but was 500,000
With 16 threads should total 500,000 but was 500,000