如果int递增/递减操作在Java 6中不是原子性的,也就是说,它们被认为是在几个步骤中执行的(读取值,递增,写入等),我希望看到一段代码,它将演示多个线程如何影响单个int变量,从而完全破坏它。
例如,基本步骤包括但不包括所有这些:i++ ~=将I放入寄存器;增量1(包括asm操作);把I写回内存;
如果两个或多个线程在进程中交错,这可能意味着在两次后续调用i++之后的值将只增加一次。
你能在java中演示一段代码来模拟多线程环境中的这种情况吗?
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
代码如下:对不起,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。
现在用AtomicInteger
和incrementAndGet()
法代替int
。它将始终打印100000,没有很大的性能影响(它使用CPU CAS指令,没有Java同步)。
您需要运行多次迭代的测试,因为++
是快速的,并且可以在出现问题之前运行到完成。
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