锁定问题:需要一些集思广益的建议
我有服务器服务,如果满足某个条件,它会释放表数据库。看起来像这样
public static void autoUnloadDbTable(final Configuration conf) {
final String mKey = getMapKey(conf);
final Table table = TABLES.get(mKey);
if (table != null) {
dblock.getLock().lock();
table.instance.dispose();
TABLES.remove(mKey);.....(2)
}
//here release lock when done...
}
但是当上面的函数执行时,还有另一种操作可以横向运行
public Table getTableLocked(final Lock lock) throws FactFinderException {
boolean loadTable = true;
// Get the current table instance
Table table = TABLES.get(mapKey);
// .....(1)
if (table != null) {
//do something
}
if (loadTable) table = loadTableLocked(table);
// lock the table, so the instance don't gets replaced
if (lock != null) lock.lock();
return table.instance;
}
在 (1) 处,我需要放置一些东西,因为如果一个线程正在执行 (2) 处的代码,而另一个线程在线程 (2 )已经有一个表对象,但由于(2)正在执行,所以它将删除表对象,然后(1)之外的所有内容都将没有正确的值......请有任何建议......?
I have server service which releases the table database if a certain criterian is satisfied. It looks like this
public static void autoUnloadDbTable(final Configuration conf) {
final String mKey = getMapKey(conf);
final Table table = TABLES.get(mKey);
if (table != null) {
dblock.getLock().lock();
table.instance.dispose();
TABLES.remove(mKey);.....(2)
}
//here release lock when done...
}
But there is another operation which can run sideways when the above function is being executed
public Table getTableLocked(final Lock lock) throws FactFinderException {
boolean loadTable = true;
// Get the current table instance
Table table = TABLES.get(mapKey);
// .....(1)
if (table != null) {
//do something
}
if (loadTable) table = loadTableLocked(table);
// lock the table, so the instance don't gets replaced
if (lock != null) lock.lock();
return table.instance;
}
At (1) i need to put something because if one thread is executing the code at (2) and the other thread is at thread (2) which already have a table object but as (2) is executing so it will delete the table object and then everything beyond (1) will not have a correct value....any suggestions please...??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要围绕表进行读/写锁定。在执行 TABLES.get(...) 来使用 table 中的内容之前获取读锁,并在执行 TABLES.get(...) 删除数据之前获取写锁。
另一种方法是每个表都有一个锁。在 TABLES.get(...) 获取表特定锁后,请确保该表仍在 TABLES 中,然后使用该锁执行操作。该表特定锁也可以是读/写锁,那么只有希望删除的代码才会获取写锁。当您尝试添加到表时,这会变得复杂,您需要聪明,并且可能对表使用 ConcurrentMap,并使用 putIfAbsent。
You probably want a read/write lock around TABLES. Acquire a read lock before you do TABLES.get(...) to use something from tables , and acquire a write lock before you do TABLES.get(...) to delete from it.
The alternative is to have a lock per table. After a TABLES.get(...) acquire the Table specific lock, then make sure the Table is still in TABLES, then perform your action with the lock. This Table specific lock can be a read/write lock as well, then only the code wishing to delete will acquire a write lock. This gets complicated when you try to add to TABLES, you will need to be clever, and probably use a ConcurrentMap for TABLES, and use putIfAbsent.
为什么不直接使用ConcurrentHashMap,它效率更高。
我认为您可以利用 ConcurrentHashMap 的锁定机制并删除您自己的锁定机制。所以
删除第 1 部分中的第一个:
第 2 部分如下所示:
}
Why not just use a ConcurrentHashMap, it is more efficient.
I think you can utilize ConcurrentHashMap's lock mechanism and remove your own. So
remove first in section 1:
Section 2 is like this:
}
根据评论,您可以考虑包装您的表:
然后,当您想要删除表时,您可以使用
myLocakbleTable.lock.writeLock().lock()
。其余时间,当您从表中读取数据时,您将使用myLockableTable.lock.readLock().lock()
Based on the comments you could consider wrapping your tables:
Then when you want to delete the table you take the
myLocakbleTable.lock.writeLock().lock()
. Rest of the time when you are reading from the table you take themyLockableTable.lock.readLock().lock()