锁定问题:需要一些集思广益的建议

发布于 2024-10-26 08:27:30 字数 985 浏览 1 评论 0原文

我有服务器服务,如果满足某个条件,它会释放表数据库。看起来像这样

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空‖城人不在 2024-11-02 08:27:30

您可能需要围绕表进行读/写锁定。在执行 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.

分開簡單 2024-11-02 08:27:30

为什么不直接使用ConcurrentHashMap,它效率更高。

我认为您可以利用 ConcurrentHashMap 的锁定机制并删除您自己的锁定机制。所以
删除第 1 部分中的第一个:

public static void autoUnloadDbTable(final Configuration conf) {
    final String mKey = getMapKey(conf);
    final Table table = TABLES.remove(mKey);
    if (table != null) {
      table.instance.dispose();
    }
}

第 2 部分如下所示:

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);
    return table.instance;
}

}

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:

public static void autoUnloadDbTable(final Configuration conf) {
    final String mKey = getMapKey(conf);
    final Table table = TABLES.remove(mKey);
    if (table != null) {
      table.instance.dispose();
    }
}

Section 2 is like this:

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);
    return table.instance;
}

}

北风几吹夏 2024-11-02 08:27:30

根据评论,您可以考虑包装您的表:

public class LockableTable {
 public Table table;
 public ReadWriteLock lock;
 public LockableTable(Table t){
   table = t;
   lock = ....
 }
}

然后,当您想要删除表时,您可以使用myLocakbleTable.lock.writeLock().lock()。其余时间,当您从表中读取数据时,您将使用 myLockableTable.lock.readLock().lock()

Based on the comments you could consider wrapping your tables:

public class LockableTable {
 public Table table;
 public ReadWriteLock lock;
 public LockableTable(Table t){
   table = t;
   lock = ....
 }
}

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 the myLockableTable.lock.readLock().lock()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文