gpt4 book ai didi

java - JIT 会优化这段代码吗?是否需要同步?

转载 作者:行者123 更新时间:2023-11-30 05:57:29 29 4
gpt4 key购买 nike

下面是一个类,其中包含拼写错误到正确拼写术语的映射。 quartz 作业通过调用 updateCache() 定期更新 map 。 updatecache 方法处理输入映射中的键和值并将它们存储在临时映射对象中。处理完成后(在 for 循环之后),它将临时映射分配给本地类变量 misspelledToCorrectlySpelled。

package com.test;

导入java.util.HashMap;导入java.util.Map;

导入org.checkthread.annotations.ThreadSafe;

@ThreadSafe公共(public)类 SpellCorrectListCacheManager {

private Map<String, String> misspelledToCorrectlySpelled = 
new HashMap<String, String>(0);

/*
* invoked by a quartz job thread
*/
public void updateCache(Map<String, String> map) {

Map<String, String> tempMap = new HashMap<String, String>(map.size());

for (Map.Entry<String, String> entry : map.entrySet()) {
//process key and values
String key = entry.getKey().toLowerCase();
String value = entry.getValue().toLowerCase();

tempMap.put(key, value);
}

// update local variable
this.misspelledToCorrectlySpelled = tempMap;
}

/*
* Could be invoked by *multiple* threads
*/
public Map<String, String> getMisspelledToCorrectlySpelled() {
return misspelledToCorrectlySpelled;
}

}

问题1:JIT优化会优化这段代码吗?

实际代码

/*
* since tempMap is assigned to misspelledToCorrectlySpelled and not
* used anywhere else, will JIT remove tempMap as shown in the optimized
* version below?
*/
Map<String, String> tempMap = new HashMap<String, String>(map.size());

for (Map.Entry<String, String> entry : map.entrySet()) {
// process key and values
String key = entry.getKey().toLowerCase();
String value = entry.getValue().toLowerCase();

tempMap.put(key, value);
}

this.misspelledToCorrectlySpelled = tempMap;

优化代码

this.misspelledToCorrectlySpelled = new HashMap<String, String>(map.size());

for (Map.Entry<String, String> entry : map.entrySet()) {
//process key and values
String key = entry.getKey().toLowerCase();
String value = entry.getValue().toLowerCase();

this.misspelledToCorrectlySpelled.put(key, value);
}

问题 2:假设 JIT 不会优化代码,是否应该同步方法 getMisspelledToCorrectlySpelled?

/*
* is this assignment atomic operation?
*
* Does this needs to be synchronized?
*
* By not synchronizing, the new map may not
* be visible to other threads *immediately* -- this is
* ok since the new map will be visible after a bit of time
*
*/
this.misspelledToCorrectlySpelled = tempMap;
}

最佳答案

您应该使用 AtomicReference存储新 map ,以避免同步和可见性问题。但代码中最大的问题是您向多个线程授予对非线程安全可变映射的访问权限。您应该将您的 map 包装成不可修改的 map :

private AtomicReference<Map<String, String>> misspelledToCorrectlySpelled = 
new AtomicReference<Map<String, String>>(Collections.unmodifiableMap(new HashMap<String, String>(0)));

/*
* invoked by a quartz job thread
*/
public void updateCache(Map<String, String> map) {

Map<String, String> tempMap = new HashMap<String, String>(map.size());

for (Map.Entry<String, String> entry : map.entrySet()) {
//process key and values
String key = entry.getKey().toLowerCase();
String value = entry.getValue().toLowerCase();

tempMap.put(key, value);
}

// update local variable
this.misspelledToCorrectlySpelled.set(Collections.unmodifiableMap(tempMap));
}

/*
* Could be invoked by *multiple* threads
*/
public Map<String, String> getMisspelledToCorrectlySpelled() {
return misspelledToCorrectlySpelled.get();
}

回答有关 JIT 优化的问题:不,JIT 不会删除临时映射的使用。

关于java - JIT 会优化这段代码吗?是否需要同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5655769/

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