Synchronous write to same file using different file pointers

Discussion in 'Java' started by Alexander Dünisch, Apr 29, 2008.

  1. Hi newsgroup,

    i have written a piece of code like the following:


    // ======= start of code sample ===============

    public void writeStuff(byte[] stuff, long location)
    throws IOException {

    RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
    try {
    raf.seek(location);
    raf.write(stuff);
    } finally {
    raf.close();
    }
    }

    // ======= end of code sample ===============


    This function is being called by multiple threads.
    Let's assume it can be guaranteed by the choice of value for location
    and the length of array 'stuff', that one thread doesn't
    overwrite the bytes written by another thread.

    Because the RandomAccessFile is local to the method,
    each thread would create it's own instance and use the associated
    file pointer.

    Can this be done safely or do i have to put the call to write in a
    synchronized block?
    In other words:
    Can two or mor threads write synchronously to different locations of
    the same file using different RandomAccessFile objects and thus
    different file pointers, if there is no risk of one thread overwriting
    another's data?

    Thanks in advance for your answers.

    Alex D.
    Alexander Dünisch, Apr 29, 2008
    #1
    1. Advertising

  2. Alexander Dünisch wrote:
    > Hi newsgroup,
    >
    > i have written a piece of code like the following:
    >
    >
    > // ======= start of code sample ===============
    >
    > public void writeStuff(byte[] stuff, long location)
    > throws IOException {
    >
    > RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
    > try {
    > raf.seek(location);
    > raf.write(stuff);
    > } finally {
    > raf.close();
    > }
    > }
    >
    > // ======= end of code sample ===============
    >
    >
    > This function is being called by multiple threads.
    > Let's assume it can be guaranteed by the choice of value for location
    > and the length of array 'stuff', that one thread doesn't
    > overwrite the bytes written by another thread.
    >
    > Because the RandomAccessFile is local to the method,
    > each thread would create it's own instance and use the associated
    > file pointer.
    >
    > Can this be done safely or do i have to put the call to write in a
    > synchronized block?
    > In other words:
    > Can two or mor threads write synchronously to different locations of
    > the same file using different RandomAccessFile objects and thus
    > different file pointers, if there is no risk of one thread overwriting
    > another's data?
    >
    > Thanks in advance for your answers.
    >
    > Alex D.
    >


    Probably. There is one concern and that is that if two threads extended
    the file you might get corrupted data. Also I don't think the
    performance decrease by synchronizing would be all that great. The disk
    is the slowest part of this and if by synchronizing you eliminated any
    partial writes and the associated seeking it might actually go quicker.

    I think I would synchronize and see if the performance is too poor. I
    have running code that uses static methods with local RandomAccessFiles
    and that use ReentrantReadWriteLocks to synchronize access. In my case
    the rate of data access is probably well below that which would cause
    great contention though.

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Apr 29, 2008
    #2
    1. Advertising

  3. Alexander Dünisch

    Christian Guest

    Alexander Dünisch schrieb:
    > Hi newsgroup,
    >
    > i have written a piece of code like the following:
    >
    >
    > // ======= start of code sample ===============
    >
    > public void writeStuff(byte[] stuff, long location)
    > throws IOException {
    >
    > RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
    > try {
    > raf.seek(location);
    > raf.write(stuff);
    > } finally {
    > raf.close();
    > }
    > }
    >
    > // ======= end of code sample ===============
    >
    >
    > This function is being called by multiple threads.
    > Let's assume it can be guaranteed by the choice of value for location
    > and the length of array 'stuff', that one thread doesn't
    > overwrite the bytes written by another thread.
    >
    > Because the RandomAccessFile is local to the method,
    > each thread would create it's own instance and use the associated
    > file pointer.
    >
    > Can this be done safely or do i have to put the call to write in a
    > synchronized block?
    > In other words:
    > Can two or mor threads write synchronously to different locations of
    > the same file using different RandomAccessFile objects and thus
    > different file pointers, if there is no risk of one thread overwriting
    > another's data?
    >
    > Thanks in advance for your answers.
    >
    > Alex D.
    >

    I have done sth like this several times. And so far had no Problem.
    Though don't expect two threads to see each others writes immediately if
    you later might add reading. If you need real nice concurrency with
    several threads writing on a single File you could simply give all
    threads access to the same FileChannel object.
    Christian, Apr 29, 2008
    #3
  4. Alexander Dünisch wrote:
    > Hi newsgroup,
    >
    > i have written a piece of code like the following:
    >
    >
    > // ======= start of code sample ===============
    >
    > public void writeStuff(byte[] stuff, long location)
    > throws IOException {
    >
    > RandomAccessFile raf = new RandomAccessFile("file.txt", "rw");
    > try {
    > raf.seek(location);
    > raf.write(stuff);
    > } finally {
    > raf.close();
    > }
    > }
    >
    > // ======= end of code sample ===============
    >
    >
    > This function is being called by multiple threads.
    > Let's assume it can be guaranteed by the choice of value for location
    > and the length of array 'stuff', that one thread doesn't
    > overwrite the bytes written by another thread.
    >
    > Because the RandomAccessFile is local to the method,
    > each thread would create it's own instance and use the associated
    > file pointer.
    >
    > Can this be done safely or do i have to put the call to write in a
    > synchronized block?


    The answer isn't simple. For short writes under some specific number of
    bytes (that will depend on the combination of OS/filesystem) you can
    expect the writes to be atomic. I would recommend against using that
    possibility. I'd write something to guard the access to that file that
    would allow to queue writes that would then be performed synchronously.

    Also, I don't think it's a good idea to open the file every time you
    want to write something. Can't you just open the file (or the wrapper
    class that would guard the access to the file) once, and then inject
    that instance to all threads that want to use it?

    > In other words:
    > Can two or mor threads write synchronously to different locations of
    > the same file using different RandomAccessFile objects and thus
    > different file pointers, if there is no risk of one thread overwriting
    > another's data?


    I've never done that but from what I know using NIO you can mmap a file
    and then lock specific areas of that file before performing write
    operations.

    > Thanks in advance for your answers.


    You're welcome.

    Regards,
    Leonard

    --
    Simplicity is the ultimate sophistication.
    -- Leonardo da Vinci
    Leonard Milcin, Apr 29, 2008
    #4
  5. I would just create a separate RandomAccessFile for each Thread. In
    what you are saying there would be no point to multi-threading because
    if only one thread can access the RAF at a time you might as well get
    rid of the multi-threading.
    Chase Preuninger, Apr 30, 2008
    #5

  6. >If you need real nice concurrency with
    >several threads writing on a single File you could simply give all
    >threads access to the same FileChannel object.


    This would certainly be the best approach.
    Unfortunately, this is not an option for me.
    Alexander Dünisch, May 3, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher Brewster
    Replies:
    5
    Views:
    333
    John Machin
    Nov 14, 2008
  2. manu
    Replies:
    5
    Views:
    377
    Nick Keighley
    Sep 17, 2009
  3. bluebaron
    Replies:
    3
    Views:
    726
    Jonathan N. Little
    Nov 4, 2009
  4. Rickert
    Replies:
    0
    Views:
    644
    Rickert
    Oct 6, 2011
  5. Guest
    Replies:
    2
    Views:
    171
    Foo Man Chew
    Dec 29, 2003
Loading...

Share This Page