• 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

giving access to only one object to read a file and write it

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to know if multiple threads try to access a single txt file, how to restrict it? If thread A tries to access the file till it completes the reading and writing part, other threads must wait. Here is what I tried.



Thanks in advance
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
this code is working or not? you can check it that whatever function you have written that is synchronized or not.or you
can File Locker in java.this link might help you.

http://www.javabeat.net/examples/2007/10/26/locking-files-using-java/
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahana mithra wrote:I would like to know if multiple threads try to access a single txt file, how to restrict it? If thread A tries to access the file till it completes the reading and writing part, other threads must wait. Here is what I tried.



Thanks in advance



There is some weird code in the application. First, it creates and starts threads that does absolutely nothing -- line 43 to line 45. Second, these thread objects do get passed to the readFromFile() method, but the method does nothing with it -- it just uses local variable (which held the thread parameter) for something else.

Henry
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sahana mithra wrote:If thread A tries to access the file till it completes the reading and writing part, other threads must wait.


Well, all you need to do is write a method which does file reading-writing, and then call that logic from a synchronized block. That's it (of course, provided you synchronized it on proper object).

As of now, I've not completely understood your code (which is slightly difficult due to creating 3 useless threads and further killing those references inside another method - which creates and starts another (three) threads).

Not only this, what you are doing is - starting a thread, and passing that object to a method. Inside that method, you are simply killing that reference by assigning a new object to it. Can you please clarify which design choices were made here? And why?

Further to this (these things don't affect code logic directly though), your class name sounds more like a method name than class name (how about MultiThreadedFileReader etc?). Also, why that class is inside singleton package? It's not a singleton class (so far).

I hope this helps.
 
No, tomorrow we rule the world! With this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic