Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sahana mithra
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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
 
Eshwin Sukhdeve
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • 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/
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic