Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

data cache sync "current thread not owner" IllegalMonitorStateException

 
YungSheng Chang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if anyone has the same problem facing me:
In Data class, I use java.nio.ByteBuffer as data cache object.
To keep data synchronization and file level lock, I put a contract on the database file access process:
1. acquire the lock of data cache object
2. read/write operation for database file
3. release the lock of data cache object
This works fine for update(), delete() and read().
But at create(), I have problem because the byte buffer capacity must be expanded.
The pseudo code looks like this:
public synchronized long createRecord(String[] data){
//acquire the exclusive use of data cache
synchronized(dataCache){
//transfer user data from String array to byte array
byte[] userData = getByteArray(data);
//use a temp data cache to add new user data at end
ByteBuffer tempDataCache = ......
//process write I/O
try{
writeToFile(dataFile, tempDataCache);
}catch(FileNotFoundException fnfe){
//handle exception 1......
//handle exception 2......
}
//reassign data cache with temp data cache
dataCache = tempDataCache; //<====problem comes here
dataCache.notifyAll(); //<====Exception in thread "main" java.lang.IllegalMonitorStateException: current thread not owner
}
}
I understand the reason why this IllegalMonitorStateException is thrown. It is because byte buffer is referenced to the other memory address than it is locked. Then notifyAll() fails.
But under my data access contract, all assignment must done in synchornization block.
Is there any way to do with it? Is my data access contract fundamentally incorrect? thanks!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic