我有一个类型为Collections.synchronizedMap的地图。该地图包含数据库表。锁定问题
现在我有3个方法和两个锁。第一个锁定在地图中的每个数据库表周围。第二个锁定在地图周围。我知道它的同步地图,所以我不需要锁,但我需要它,因为其他原因。
method1()
{
calls readlock on a table (first lock)
try{
if(certain condition is satisfied)
{
call method2
}
}finally{
realease readlock on the table (first lock)
}
}
method2(){
call write lock on the map first (second lock)
call write on the table now(first lock)
try{
//do stuff
}finally{
unlock write lock on table.
unlock writelock on map.
}
}
Method3(Lock){ //This lock is lock on tables ,can be read as well as write,but unlocked now
grab readlock on map first
try{
//do stuff
Lock.lock() //lock the table lock now
}finally{
realease the readlock now on map
}
我得到一个封闭的情况时,在方法1一readlock发生在表,然后当越来越地图写后方法2 ...
我是不是做错了什么地方?
请编辑您的问题(包括标题)以删除SCREAMING部分。阅读起来很难。如果您也正确格式化代码,可能会有所帮助。 – Mat 2011-04-01 11:31:02
R/W锁是可重入锁。它们不是可升级的。 – 2011-04-01 11:38:07
一些正确的代码格式可以让你更容易地遵循你的代码。 – jmg 2011-04-01 11:39:04