gpt4 book ai didi

java - 你必须同步并发集合吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:21 24 4
gpt4 key购买 nike

想象一下下面的例子:一个应用程序启动两个线程。 Provider 类保存并发集合并将数据写入其中。消费者从集合中读取数据。

下面的代码是正确的还是我必须添加同步?

public class Application{
public static void main(String...args) throws Exception{
Provider p = new Provider();
new Thread(p).start();
new Thread(new Consumer(p)).start();

// Make sure the example stops after 60 seconds
Thread.sleep(1000*60);
System.exit(0);
}
}

/**The Provider (writes data to concurrent collection)*/
class Provider implements Runnable{

private ConcurrentMap<Integer, String> map
= new ConcurrentHashMap<Integer, String>(20, 0.5f, 1);

public void run(){
Integer i = 1;
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException ignore) {
}
// Synchronization ?
map.put(i, i.toString());
i++;
}
}

public ConcurrentMap<Integer, String> getMap(){
// Synchronization ?
return map;
}

}

/**The Consumer (reads data from concurrent collection)*/
class Consumer implements Runnable{

private Provider provider;

public Consumer(Provider p){
provider = p;
}

public void run(){
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
}
// Synchronization ?
ConcurrentMap<Integer, String> m = provider.getMap();
if(m!=null)
for(String s: m.values())
System.out.print(s);
System.out.println();
}
}

}

最佳答案

来自ConcurrentHashMap documentation :

For aggregate operations such as putAll and clear, concurrent retrievals may reflect insertion or removal of only some entries. Similarly, Iterators, Spliterators and Enumerations return elements reflecting the state of the hash table at some point at or since the creation of the iterator/enumeration. They do not throw ConcurrentModificationException. However, iterators are designed to be used by only one thread at a time. Bear in mind that the results of aggregate status methods including size, isEmpty, and containsValue are typically useful only when a map is not undergoing concurrent updates in other threads. Otherwise the results of these methods reflect transient states that may be adequate for monitoring or estimation purposes, but not for program control.

所以您不需要同步,因为您不会得到ConcurrentModificationException。您是否想要取决于您的程序逻辑。

关于java - 你必须同步并发集合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793433/

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