This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I'm testing my lock schema, creating several threads to delete one data record. My problem is... the first thread write the deleted flag to file, but the second thread read this flag as "not deleted". The acess to the file is synchonized.
In other words... The "write" of the first thread fails! Is JVM doing a cache of the file in memory?
It's a bit difficult to help you without seeing the code, what mode are you using for the RandomAccessFile contructor and how are you writing the delete flag? Is the move to the correct position in the file and also the write synchronized?
If can post a small segment of your write method may be we can help further.
// Write flag data byte flag = XXXX file.writeByte( flag ) ; }
Joined: Dec 04, 2001
Your code for writing the invalid flag looks okay, does the record eventually get marked for deletion by any of the threads? as I'm wondering if there is a problem with your locking mechanism, i.e, could more than one thread read the flag before it's been marked for deletion?
Jason [ September 01, 2006: Message edited by: Jason Moors ]
...i'm assuming your locking is alright and i'm also taking a guess that the problem may be with your file pointer position- the two threads may be accessing different positions in your file. try writing (and reading) characters to the file... that way you can open the file in a text editor to see what's going on- you may even manually edit the file yourself to see what happens. ...some other thing some of the bigger boys do (which could make your code a little fuller) is to do some logging (see: ).
Please treat the following a a little brainstorm before the first cup of strong coffee this morning.
Ok, you have done file.writeByte(flag). Does that write have to be done immediately? Like right now? How can you ensure that it is?
(I might be a little hasty on that one, I'll just check the API retrospectively)
Yep I was :roll: The "s" in your mode should ensure the file gets flushed. There would be no harm in trying a file.flush() just in case the OS is not honouring the implicit flush. But there is no flush() method on RandomAccessFile.
So it's going to be something else.
What OS are your using? No, that's not going anywhere.
Is there just one instance of that RandomAccessFile object (file) you are synchronizing the threads on?
In my assignment the delete flag is two bytes, yours is just one byte? [ September 02, 2006: Message edited by: Barry Gaunt ]
I just wrote a test program starting 500 reader threads synching on one RandomAccessFile instance. As soon as the main thread (also synched on the single file object) changed the value in the file, the reader threads noticed it. That was on UBUNTU linux.
Luiz Reginaldo Curado
Joined: Jan 19, 2006
Thanks for your replys! My error was an exception in another place of the code in wich I had not put an printStackTrace...
But I found some real bugs in my locking schema, and thanks to you I have correted it.