• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Can two threads access the same file concurrently?

 
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
 
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.
 
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.
 
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,
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic