我有一個類型爲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