It's not a secret anymore!*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Thread Safety & RandonAccessfile Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Thread Safety & RandonAccessfile" Watch "Thread Safety & RandonAccessfile" New topic
Author

Thread Safety & RandonAccessfile

Thirumurugan Mylrajan
Ranch Hand

Joined: Jan 26, 2006
Posts: 64
Hi All,

First of all an Into: Since I am using RMI I have no control over the threads. I have determined that my data class is thread safe(No midification of the class variables inside the methods) and client identification not a problem because of use of cookies. So I am using only a single data class.

But inside the data class I am confused whether to use a single RandomAcces file or one instance of it per method. When I look into RA class, there are only two class variables.

private FileDescriptor fd;
private FileChannel channel = null;
But this will go an for ever as I have to determine if FileDescriptor is thread safe...

Is this too much for the assignment. Can I just put an end to this and create RA instances for each method?.

Thanks for your time.


SCJP , SCJD. (IBM 142 in progress).
Oliver Weikopf
Ranch Hand

Joined: Feb 17, 2004
Posts: 58
I'm not sure I understand your problem correctly. If you use only one RA file (like I do), you'll definitely have to make sure that no two methods are called synchronously, as inside a method, you'll probably set the file pointer to a position and the read or write there. This is something you can't allow two threads to do at the same time. So, synchronizing your Data methods would be necessary, no matter whether RAFile is thread safe or not.
Daniel Dalton
Ranch Hand

Joined: Mar 20, 2005
Posts: 146
Alternatively, if you choose to create a new RandomAccessFile object per method, you need to be aware that the number of FileDescriptors available within the system is Operating System dependant - or at the very least not unlimited. That means two things:

1) You may run out of FileDescriptors (which may or may not be acceptable to you).

2) This will probably happen with different numbers of users depending on the underlying OS.

However, if neither thing happens before having an unrealistic number of users, you may be happy to accept the limitation and just document your choices.
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi, Thirumurugan

1.Why you use FileChannel I don't think that nio is allowed.
2.You can use only one (synchronized) RAF for all your clients.
3.You can also use more than one RAFs but you need to find a way to synchronize
them, thats why the solution from 2 is easy/better.

Regads M.


SCJP, SCJD, SCWCD, OCPJBCD
Thirumurugan Mylrajan
Ranch Hand

Joined: Jan 26, 2006
Posts: 64
Originally posted by Oliver Weikopf:
I'm not sure I understand your problem correctly. If you use only one RA file (like I do), you'll definitely have to make sure that no two methods are called synchronously, as inside a method, you'll probably set the file pointer to a position and the read or write there. This is something you can't allow two threads to do at the same time. So, synchronizing your Data methods would be necessary, no matter whether RAFile is thread safe or not.


What you mentioned is exactly the problem I would like to solve.And I want to avoid synchronization in the data methods. So I am trying to add one RAF per method so that they dont interfere with each other.

Is this an overkill compared to synchronization?..
Thirumurugan Mylrajan
Ranch Hand

Joined: Jan 26, 2006
Posts: 64
Originally posted by Daniel Dalton:
Alternatively, if you choose to create a new RandomAccessFile object per method, you need to be aware that the number of FileDescriptors available within the system is Operating System dependant - or at the very least not unlimited. That means two things:

1) You may run out of FileDescriptors (which may or may not be acceptable to you).

2) This will probably happen with different numbers of users depending on the underlying OS.

However, if neither thing happens before having an unrealistic number of users, you may be happy to accept the limitation and just document your choices.



Thank you..I never considered about this. I guess its not very high in a Windows environment. Since I want to put an end to my SCJD quest by next weekend, I would rather mention this in my choices.txt.
Thirumurugan Mylrajan
Ranch Hand

Joined: Jan 26, 2006
Posts: 64
Originally posted by Mihai Radulescu:
Hi, Thirumurugan

1.Why you use FileChannel I don't think that nio is allowed.
2.You can use only one (synchronized) RAF for all your clients.
3.You can also use more than one RAFs but you need to find a way to synchronize
them, thats why the solution from 2 is easy/better.

Regads M.


Thanks all of you for your replies.

1. The FileChannel is present inside RAF.. anyway I dont know nio..
2. I dont quite understand by synchronized RAF.. I dont know how to do it.. maybe you meant synchronized data methods?.
3. Since all the file modificaion operations are controlled by lock manager I dont think it is necessary to sync RAF operations during read..
 
 
subject: Thread Safety & RandonAccessfile