aspose file tools*
The moose likes Threads and Synchronization and the fly likes giving access to only one object to read a file and write it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "giving access to only one object to read a file and write it" Watch "giving access to only one object to read a file and write it" New topic
Author

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

sahana mithra
Ranch Hand

Joined: Oct 26, 2010
Posts: 72
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

Joined: Mar 15, 2012
Posts: 80
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
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: giving access to only one object to read a file and write it