gpt4 book ai didi

java - 为什么当我在此 java 代码中执行读取-更新-写入时不存在竞争条件?

转载 作者:行者123 更新时间:2023-12-02 13:17:13 26 4
gpt4 key购买 nike

我的代码有许多线程对共享 HashMap 进行读取-更新-写入操作,而无需任何同步。我用 1000 个线程、1M 迭代运行了很多次,所以我预计 4 个键的最终值将在 250M 左右,但不完全是这样,因为更新/写入可能会丢失,因为多个线程由于读取而读取相同的值同时。然而,经过多次试验,我发现这些值始终是 250M,因此没有丢失更新。有人请帮帮我。 FWIW,我正在编写这个玩具应用程序,以将其与使用 Collections.syncronizedMap 时进行比较,但是,我需要这个应用程序失败(以显示竞争条件),但它似乎总是产生无更新丢失的结果。

Work took: 39.496

Key: 0 val: 250000000

Key: 1 val: 250000000

Key: 2 val: 250000000

Key: 3 val: 250000000

package threadsafecollections;

import java.util.HashMap;
import java.util.Map;

public class SynchronizedCollectionTask {

public static int NUM_THREADS = 1000;

public static class ReadUpdateWrite implements Runnable {

Map<Integer, Integer> map;
int threadId;
public static int NUM_ITERATIONS = 1000000;

ReadUpdateWrite(Map<Integer, Integer> m, int threadId) {
this.map = m;
this.threadId = threadId;
}

@Override
public void run() {
for (int i = 0; i < NUM_ITERATIONS; i++) {
int key = threadId % 4;
Integer val = this.map.get(key);
map.put(key, val == null ? 1 : val + 1);
}

// At this point I expect each key in map to have value of CLOSE TO
// numThreads * NUM_ITERATIONS / 4 = 250M. I expect some threads to have
// both read the same value, and therefore "miss" a write. For example,
// thread 4 and 8 both read key = 0 and see value = 3. Now both thread
// increment value to 4, instead of one thread incrementing to 4 and the
// other incrementing to 5.
}

}
public static void main(String[] args) throws InterruptedException {
Map<Integer, Integer> sharedMap = new HashMap<Integer, Integer>();



// Initialize threads
Thread[] readers = new Thread[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
readers[i] = new Thread(new ReadUpdateWrite(sharedMap, i));
}

long start = System.currentTimeMillis();

// Start threads
for (int i = 0; i < NUM_THREADS; i++) {
readers[i].run();
}

// Join threads
for (int i = 0; i < NUM_THREADS; i++) {
readers[i].join();
}

long end = System.currentTimeMillis();

System.out.println("Work took: " + (end - start) / 1000D);

for (int key : sharedMap.keySet()) {
System.out.println ("Key: " + key + " val: " + sharedMap.get(key));
}
}
}

最佳答案

恭喜:您已经编写了线程安全代码,因为 Thread.run() 只是启动内部 Runnable.run,因此您可以按顺序调用所有操作。使用Thread.start()代替:

这是 Thread.run() 的样子:

@Override
public void run() {
if (target != null) {
target.run();
}
}

关于java - 为什么当我在此 java 代码中执行读取-更新-写入时不存在竞争条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43733591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com