Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: contractor - file io and threads

 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've seen discussion here about the record locking and using unreferenced to check when a client vanishes while holding a lock. I have not, however, seen any coverage of any requirement to single-thread all io on the file. It seems to me we also have to ensure the io is single-threaded to avoid the situation described below:
thread 1 set file pointer
thread 1 read rec
thread 2 set file pointer
thread 1 modify rec
Also, I am unfamiliar with the unreferenced interface. I assume it calls the unreferenced method when an RMI client vanishes?
ms
 
Gareth Knowles
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike
I have separate private methods to move the file pointer, read from the file and write to the file. I synchronize on any public method that uses the movePointer(), read(0, or write() methods. I've tested many times with 20 clients running 50 read/writes and not seen any data corruption (yet!).
HTH
Gareth.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,
I believe you are right about having a single thread (or a single instance of a class) to do file operations. Making methods that move the file pointer and then do reads or writes atomic is a very good idea - as Gareth mentioned.
Also, I am unfamiliar with the unreferenced interface. I assume it calls the unreferenced method when an RMI client vanishes?

Correct. When there are no more references to a remote object, (so the rmiregistry does not have a reference, and the client is no longer connected) then unreferenced is called, and you can do any cleanup code you want to.
Another way of handling clients vanishing while holding locks is to use a WeakHashMap - you can look at any of Max's posts for more information on how to do that.
Regards, Andrew
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic