Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile pointer question

 
mick jackman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to use one single RandomAccessFile

I know that I need to synchronize access to the RandomAccessFile as there is only 1 file pointer in some way.

Which scenario should be used?

scenario A

Only 1 thread is able to read or write at the same time. So for example if one thread is reading, no other threads can read or write until the
reading thread has finished reading the file.

or

scenario B

Thread 1 can read whilst Thread 2 is writing to the file?
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to use one single RandomAccessFile

One should always use only a single writer to a file in general. Readers can be multiple. However, if you are using a single reader/stream then it will be aweful to allow two threads to access it concurrently. You can use FileLocks to ensure that only one thread reads a file(or part of it) at a time.
Having said the above, why do you want multiple threads to read from the same file instance? You can provide an entity(basically a wrapper over the RandomAccessFile) that fetches the data from the file and all the other components always refer to this entity to get the data. This entity can either read the data in one go and then have in-memory representation of the same OR it can sequentially allow people to read from the file. Also, it will help you to move to any other Stream implementation later without affecting the clients. It can have separate locks for reading and writing if the need be.

[ August 10, 2007: Message edited by: Nitesh Kant ]
[ August 10, 2007: Message edited by: Nitesh Kant ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitesh Kant:

You can use FileLocks to ensure that only one thread reads a file(or part of it) at a time.


Sure?

From the java.nio.channels.FileLock api:
"File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine."
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:


Sure?

From the java.nio.channels.FileLock api:
"File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine."

Ooops my bad ... its for controlling access to a file by multiple programs and its OS dependent.
Sorry for that.
 
Romeo Kienzler
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
please take care, you are not allowed to use classes from the java.nio.*-Package.

This only as a hint

Greetings,
Romeo
[ August 15, 2007: Message edited by: Romeo Kienzler ]
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Romeo,

That's not strictly true, not all assignments forbid the use of NIO package, if you search your HTML instructions and it doesn't mention NIO you are able to use it.

Use of NIO Package

Regards
Jason
[ August 15, 2007: Message edited by: Jason Moors ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic