ReentrantReadWriteLock 阻止我对对象执行操作?
ReentrantReadWriteLock blocking me from act on an object?
我有以下对象:
public class DataStructures {
public Map<String, User> registeredUser;
private ReadWriteLock readWriteLock;
public DataStructures() {
this.registeredUser = new ConcurrentHashMap<>();
readWriteLock = new ReentrantReadWriteLock(true);
}
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
}
我有以下块:
if(user != null && user.status()) {
String userNameList = "";
dataStructures.getReadWriteLock().readLock().lock();//ReentrantReadWriteLock
List<User> userList = new ArrayList<>(dataStructures.registeredUser.values());
//Will perform a sort on the userlist according their initial registration number
Collections.sort(userList, Comparator.comparing(User::getRegistrationId));
for (User tmpUser : userList) {
userNameList += tmpUser.getUsername() + " ";
}
connections.send(connectionId, "10 " + "7 " + userList.size() + " " + userNameList);
dataStructures.getReadWriteLock().readLock().unlock();//ReentrantReadWriteLock
}
如您所见,我锁定了这段代码的一部分,以便让线程在没有人尝试写东西的时候从这段代码中读取。
此外,我有以下代码:
dataStructures.getReadWriteLock().writeLock().lock();//ReentrantReadWriteLock
if(dataStructures.registeredUser.putIfAbsent(username, new User(username, password, dataStructures.registraionId)) == null) {
connections.send(connectionId, "10 " + "01");
dataStructures.registraionId++;
}
else
connections.send(connectionId, "11 " + "01");
dataStructures.getReadWriteLock().writeLock().unlock();//ReentrantReadWriteLock
现在,我知道当第一个代码块发生时,第二个代码块将等待第一个代码块完成,我的问题是否在下一个代码块中我将能够同时从 dataStructure
对象读取,尽管我没有提到任何 ReentrantReadWriteLock 锁:
String username = seperate[1];
String password = seperate[2];
User user = dataStructures.registeredUser.get(username);
提前致谢!
是的,您将能够从 dataStructure 对象读取,同时有人将持有锁定进行写入或读取,因为您正在为 registeredUser 使用线程安全的 ConcurrentHashMap。
如果您对 registeredUser 使用普通的 HashMap,您仍然可以从 registeredUser 读取数据,但您会看到 incorect/stale 数据,因为普通的 HashMap 不是线程安全的,并且您在没有锁定。
我有以下对象:
public class DataStructures {
public Map<String, User> registeredUser;
private ReadWriteLock readWriteLock;
public DataStructures() {
this.registeredUser = new ConcurrentHashMap<>();
readWriteLock = new ReentrantReadWriteLock(true);
}
public ReadWriteLock getReadWriteLock() {
return readWriteLock;
}
}
我有以下块:
if(user != null && user.status()) {
String userNameList = "";
dataStructures.getReadWriteLock().readLock().lock();//ReentrantReadWriteLock
List<User> userList = new ArrayList<>(dataStructures.registeredUser.values());
//Will perform a sort on the userlist according their initial registration number
Collections.sort(userList, Comparator.comparing(User::getRegistrationId));
for (User tmpUser : userList) {
userNameList += tmpUser.getUsername() + " ";
}
connections.send(connectionId, "10 " + "7 " + userList.size() + " " + userNameList);
dataStructures.getReadWriteLock().readLock().unlock();//ReentrantReadWriteLock
}
如您所见,我锁定了这段代码的一部分,以便让线程在没有人尝试写东西的时候从这段代码中读取。
此外,我有以下代码:
dataStructures.getReadWriteLock().writeLock().lock();//ReentrantReadWriteLock
if(dataStructures.registeredUser.putIfAbsent(username, new User(username, password, dataStructures.registraionId)) == null) {
connections.send(connectionId, "10 " + "01");
dataStructures.registraionId++;
}
else
connections.send(connectionId, "11 " + "01");
dataStructures.getReadWriteLock().writeLock().unlock();//ReentrantReadWriteLock
现在,我知道当第一个代码块发生时,第二个代码块将等待第一个代码块完成,我的问题是否在下一个代码块中我将能够同时从 dataStructure
对象读取,尽管我没有提到任何 ReentrantReadWriteLock 锁:
String username = seperate[1];
String password = seperate[2];
User user = dataStructures.registeredUser.get(username);
提前致谢!
是的,您将能够从 dataStructure 对象读取,同时有人将持有锁定进行写入或读取,因为您正在为 registeredUser 使用线程安全的 ConcurrentHashMap。
如果您对 registeredUser 使用普通的 HashMap,您仍然可以从 registeredUser 读取数据,但您会看到 incorect/stale 数据,因为普通的 HashMap 不是线程安全的,并且您在没有锁定。