• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RandomAccessFile pointer question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic