• 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 do not write!

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all, I really need some help in this question.

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?

Please, help me with this challenge...
 
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 Luiz,

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.

regards
Jason
 
Luiz Reginaldo Curado
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
file = new RandomAccessFile("mydb.db", "rws") ;

...

synchronized(file) {

file.seek( recordPosition ) ;

// Write flag data
byte flag = XXXX
file.writeByte( flag ) ;
}
 
Jason Moors
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hiya, Luiz...

...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: ).

...hope this helps.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

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.

Thank you!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic