如果int递增/递减操作在Java 6中不是原子的,也就是说,它们被认为是在几个步骤中执行的(读取值、递增、写入等),我希望看到一段代码,它将演示多个线程如何以某种方式影响单个int变量,从而彻底破坏它。
例如,基本步骤包括但不是全部: i++ ~=将I放入寄存器;递增I (inc asm操作);将I写回内存;
如果两个或更多线程在进程中交叉,这可能意味着在两次后续的i++调用之后,该值只会递增一次。
你能用java演示一段在多线程环境中模拟这种情况的代码吗?
发布于 2011-08-12 16:21:01
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发布于 2011-08-12 16:20:01
下面是代码。对于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同步)。
发布于 2011-08-12 16:33:24
您需要运行测试多次迭代,因为++是快速的,并且可以在出现问题之前运行到完成。
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,000https://stackoverflow.com/questions/7037268
复制相似问题