I am using RandomAcessFile for my IO and its instance variable of Data class. I am using it through-out my Data class. I open the file in constructor and just leave it open (Don't know where to close).
There is sepreate instance of Data class for each client.
Will it crate any threading problem as diffrent client will access db.db same time for manuplating db.db.
My concern is as with each raf instance I move file pointer back and forth in read, delete,update, create.
As delete and create - eliminated (synchronization on Vector). But question about read and update.
"I'm not back." - Bill Harding, Twister
Personally I prefer to create a single FileChannel or RAF which all clients use, and protect access via synchronization.
SCJP,SCJD,SCWCD,SCBCD,SCDJWS,SCEA
Back to your design - truth is, I have a hard time imagining it being made into something thread-safe if every client has a separate Data instance which in turn has its own separate RAF instance which refers to a single shared file.
Maybe not at the same time, so concurrent access may not be a problem,
but the way the Java memory model works you may get some strange effects anyway if you let different threads access a single raf instance without synchronization.
"I'm not back." - Bill Harding, Twister
[PM]: What kind of "strange effects" ? Can you give an example ?
What I was thinking of is instance data that is out of data because when a previous thread changed it, it only changed a local copy of the data, and without synchronization there's been no need yet to write that changed data to main memory, so when you access the instance from a separate thread, it sees an older version of the data, missing the last thread's change. This sort of thing can crop up in all sorts of subtle an unexpected ways when two different threads access shared data without synchronization.
Or, instead of opening the file in "rw" mode you can use "rws" or "rwd" mode, both of which force writes to update the underlying file immdiately, before the write method returns.
So alright, it seems that this design can be made reasonably safe.
SCJP (1.4), SCWCD, SCJD
"I'm not back." - Bill Harding, Twister
SJCP, SCBCD, SJCD, SCDJWS, SCEA (Part I)
"I'm not back." - Bill Harding, Twister
SJCP, SCBCD, SJCD, SCDJWS, SCEA (Part I)
Originally posted by Jim Yingst:
[b][Arun]And i read the Thread pointed out by Jim. It was really confusing. Some of the posts i wasnt sure if they are talking about "1 thread - 1 FileChannel" or "multiple threads - 1 file channel".
[Jim}Yea, sorry about that. In general I was trying to focus on multiple threads - 1 FileChannel in that discussion, and explaining why you still need synchronization in that case.
SCJP (1.4), SCWCD, SCJD
"I'm not back." - Bill Harding, Twister
[JM]
What I was thinking of is instance data that is out of data because when a previous thread changed it, it only changed a local copy of the data, and without synchronization there's been no need yet to write that changed data to main memory, so when you access the instance from a separate thread, it sees an older version of the data, missing the last thread's change. This sort of thing can crop up in all sorts of subtle an unexpected ways when two different threads access shared data without synchronization.
Philippe said:
I use "rwd".
As an aside, I think closing the raf in a finalize method is not a good idea but I have seen a number of people say they are doing this. But for those who are closing in the finalize method, it seems to me that "rws" is a must.
It seems to me that "rws" is a better choice because it also updates the metadata. Since the length of the file changes when a record is added to the end of the file, it seems like a good idea to have the metadata stay as current as the data.
RandomAccessFile's constructor javadoc:
The "rwd" mode can be used to reduce the number of I/O operations performed. Using "rwd" only requires updates to the file's content to be written to storage; using "rws" requires updates to both the file's content and its metadata to be written, which generally requires at least one more low-level I/O operation.
FileChannel.force() method javadoc (related):
Invoking this method may cause an I/O operation to occur even if the channel was only opened for reading. Some operating systems, for example, maintain a last-access time as part of a file's metadata, and this time is updated whenever the file is read. Whether or not this is actually done is system-dependent and is therefore unspecified.
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|