• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can two threads access the same file concurrently?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the answer is yes - which thread will gain access to the file pointer? and will the other thread block till it gets the file pointer?
Help is greatly appreciated
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The behaviour of two threads trying to access the same file is not defined. Its a classic place to use Java's built in "synchronization" mechanism.
 
Ben Nichols
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... They are capable of virtual concurrency.
But you shouldn't allow concurrent access for writes.
You should allow two threads to concurrently read, and make the
file interface class synchronize and block for the file pointer.
And you should block concurrent writes, since you cannot predict the final state the data will be in. I probably didn't answer your question.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help, guys.
 
Mahesh Hegde
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
Could you please elaborate a bit on 'virtual concurrency' ?
How can we block on a file pointer ? File pointer is not an object, it is part of the File object. But if we block on File object, only one thread can access a file(for reading or writing) which will really slow down the things.
Thanks,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic