• 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

Thread Safe

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any situations where you need to make your code thread safe if all it's doing is reading a file? This is kind of beyond the scope of the exam. Are there any file systems that won't allow concurrent read-only access to a file?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think multiple read operations might require thread safety, since you are not changing the contents of the file in any way.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread safety is only an issue when multiple threads concurrently read a single value AND at least one thread modifies this value at some point in time.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its not 100% that there is no synchronization problems while multiple threads reading from a single file.
ConsiderHere there is problem as a single thread can't read the file to the fullest.
So when each thread is supplying input to some other process then only a partial data of the file will be available from each thread.
This problem can't even be solved with the synchronized block, but it can be made sure that a single thread reads the whole file and other threads have no data to read at the end of first thread's execution.
To solve this problem you can have a FileReader object inside the run() method to ensure that each thread has the resource to read the same file.

But this example is an inefficient use of thread.
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic