在多线程应用中使用LinkedHashMap的get()方法安全吗
Is it safe to use get() method of LinkedHashMap in multi-thread application
我想编写一个使用 get() 方法访问 LinkedHashMap 的多线程应用程序(应用程序仅使用 get() 方法从 LinkedHashMap 获取数据)。源代码如下:
public class Thread1 implements Runnable {
LinkedHashMap<String, ArrayList<GroupMembers>> group;
public Thread1(LinkedHashMap<String, ArrayList<GroupMembers>> _group) {
group = _group
}
public void run() {
while (true) {
Set<String> groupKeyNames = group.keySet();
for (String groupName : groupKeyNames) {
ArrayList<GroupMembers> members = group.get(groupName);
/* Thread 1 processing */
...
}
}
}
}
public class Thread2 implements Runnable {
LinkedHashMap<String, ArrayList<GroupMembers>> group;
public Thread2(LinkedHashMap<String, ArrayList<GroupMembers>> _group) {
group = _group
}
public void run() {
while (true) {
Set<String> groupKeyNames = group.keySet();
for (String groupName : groupKeyNames) {
ArrayList<GroupMembers> members = group.get(groupName);
/* Thread 2 processing */
...
}
}
}
}
public class Sample {
public static void main(String[] args) throws IOException {
/* Init LinkedHashMap */
LinkedHashMap<String, ArrayList<GroupMembers>> group;
group.put("group1", new ArrayList<GroupMembers>());
group.put("group2", new ArrayList<GroupMembers>());
...
Thread1 t1 = new Thread(group);
t1.start();
Thread1 t1 = new Thread(group);
t2.start();
}
}
在上面的多线程应用中使用LinkedHashMap.get()安全吗?
引用Javadoc:
Note that this implementation is not synchronized. If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.
...
In insertion-ordered linked hash maps, merely changing the value associated with a key that is already contained in the map is not a structural modification. In access-ordered linked hash maps, merely querying the map with get is a structural modification.)
它是 insertion-ordered 还是 access-ordered 取决于您实际如何初始化它 - 这不包括在问题中:
- 如果您使用
new LinkedHashMap(int, float, boolean)
构造 LinkedHashMap
,它可能是 access-ordered(如果您将 true
作为布尔参数传递),在这种情况下它会 不是是thread-safe
- 如果您使用任何其他构造函数构造它(或将
false
作为布尔参数传递),它将是 thread-safe - 如果您也不继续使用 groups
在其他地方引用。
我想编写一个使用 get() 方法访问 LinkedHashMap 的多线程应用程序(应用程序仅使用 get() 方法从 LinkedHashMap 获取数据)。源代码如下:
public class Thread1 implements Runnable {
LinkedHashMap<String, ArrayList<GroupMembers>> group;
public Thread1(LinkedHashMap<String, ArrayList<GroupMembers>> _group) {
group = _group
}
public void run() {
while (true) {
Set<String> groupKeyNames = group.keySet();
for (String groupName : groupKeyNames) {
ArrayList<GroupMembers> members = group.get(groupName);
/* Thread 1 processing */
...
}
}
}
}
public class Thread2 implements Runnable {
LinkedHashMap<String, ArrayList<GroupMembers>> group;
public Thread2(LinkedHashMap<String, ArrayList<GroupMembers>> _group) {
group = _group
}
public void run() {
while (true) {
Set<String> groupKeyNames = group.keySet();
for (String groupName : groupKeyNames) {
ArrayList<GroupMembers> members = group.get(groupName);
/* Thread 2 processing */
...
}
}
}
}
public class Sample {
public static void main(String[] args) throws IOException {
/* Init LinkedHashMap */
LinkedHashMap<String, ArrayList<GroupMembers>> group;
group.put("group1", new ArrayList<GroupMembers>());
group.put("group2", new ArrayList<GroupMembers>());
...
Thread1 t1 = new Thread(group);
t1.start();
Thread1 t1 = new Thread(group);
t2.start();
}
}
在上面的多线程应用中使用LinkedHashMap.get()安全吗?
引用Javadoc:
Note that this implementation is not synchronized. If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.
...
In insertion-ordered linked hash maps, merely changing the value associated with a key that is already contained in the map is not a structural modification. In access-ordered linked hash maps, merely querying the map with get is a structural modification.)
它是 insertion-ordered 还是 access-ordered 取决于您实际如何初始化它 - 这不包括在问题中:
- 如果您使用
new LinkedHashMap(int, float, boolean)
构造LinkedHashMap
,它可能是 access-ordered(如果您将true
作为布尔参数传递),在这种情况下它会 不是是thread-safe - 如果您使用任何其他构造函数构造它(或将
false
作为布尔参数传递),它将是 thread-safe - 如果您也不继续使用groups
在其他地方引用。