This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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: 23909
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic