Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Stream Synchronization  RSS feed

 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I am using two programs which reads and write the same file. I need to synchronize this process so that while writing the file it should not be read (while writing). Your sugesstions regarding this is highly appreciated.

Regards,
Raghu
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could define an object that acts as a monitor for I/O to that file. Your reading and writing operations would need to get that monitor's lock before commencing. That would, however, mean that your application could spend a long time blocked, waiting to get that lock. If that is unacceptable, consider having a status variable, with thread-safe access, and checking it before attempting any read/write.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I now see that you have two separate programs, so you can't use synchronisation. However, I'm a bit suspicious about the second similar post with the same question, which has recently appeared. Is this, by any chance, a homework assignment on a particular course?! We don't do people's homework here, if we can help it.

With that in mind, I'm just going to say "maybe sentinel file or server socket" and if that's not enough clue, sorry.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!