aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes FBN : RAF handle and Synchronize methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "FBN : RAF handle and Synchronize methods" Watch "FBN : RAF handle and Synchronize methods" New topic
Author

FBN : RAF handle and Synchronize methods

Akash Singh
Ranch Hand

Joined: Aug 21, 2003
Posts: 80
I am doing FBN assignment, i thought i have completed my db package. But looking into some threads in this forum on RAF handle and synchronize methods, i started thinking i am not doing right. I have some questions, and need advice from you guys.
1. RAF handle:
In the given Data class, RandomAccessFile is instance variable. My each qui client will have unique Data object. In this case, suppose, if there are more than 300 clients, then there will be 300 file handles for open dbfile.
Would it not crash the server, when open file handles exceeds max no of allowable file handles for that OS ?.
If yes, To avoid this, do i need to make RandomAcessFile, a class level variable(i mean static RandomeAcessFile db) instead of instance variable ?

2. Synchronize methods and file I/O.
If one Data instance is shared among all clients then reading/writing to file is ok, since all methods are synchronized. Read will not be allowed unless write is completed or vice versa.
But if each client has unique Data object, in that case synchronize does not prevent one thread to read, while other thread is doing write or vice versa concurrently.
case 1,
Thread T1 asks for read record 2,
at the same time T2 asks to modify record 2.
case 2,
Thread T1 asks for modify record 2,
at the same time T2 asks to modify record 2.
How will OS handle this situation(if i do not lock the record logically), will it allow both thread to read and modify concurrently(in case 1), / allow both threads to modify(case 2) same record concurrently ?

In assignment, while modifying i have to lock-->read-->modify-->unlock. Write is fine, but reader may get records that is being modified at the same time. How should i handle this situation. Since i am planning to have one unique Data object per gui client.

3. Since i am having one Data object per client, there is no need for synchronize method. But, if i remove "synchronize" from all the methods, my Data class is not threadsafe, if single instance of Data is shared among all clients.
Assignment instruction says "Because multiple concurrent connections may exist, you must make both your server and the suncertify.db classes threadsafe." Does it mean i must make my Data class thread safe, though i am providing unique Data object per GUI client ?
Please do not get angry, if my questions do not seem to reasonable.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Hi Akash,
Would it not crash the server, when open file handles exceeds max no of allowable file handles for that OS ?.

Whether it crashes the server or not is dependant on how you code your server - do you shutdown the server if your call to the constructor of Data class throws an exception?
Regardless, even if you did handle the exception, you will not be in a good state, as there will be no file handles remaining, and unless your OS has separate limits on file handles per process, your entire server could become non operational, which is sure to gain you some new enemies.
To avoid this, do i need to make RandomAcessFile, a class level variable

That would probably solve that particular issue.
Hmmm - two questions for you (I may have asked these of you before, I don't remember):
  • Have you modified the add() method since you have multiple instances of Data class?
  • Have you considered whether changing "complete" classes is allowable?


  • But if each client has unique Data object, in that case synchronize does not prevent one thread to read, while other thread is doing write or vice versa concurrently ... How will OS handle this situation

    Well it is not really up to the OS to handle this - it should be handled at the JVM level.
    You could try setting up a couple of threads to see what happens . You will learn more from doing this than just getting a quick answer from here.
    But consider that the API for RandomAccessFile makes no statements about thread safety. So guess what that means.
    In assignment, while modifying i have to lock-->read-->modify-->unlock. Write is fine, but reader may get records that is being modified at the same time. How should i handle this situation.

    I wouldn't worry about that until you have solved the issue of two clients writing to different records simultaneously.
    Since i am having one Data object per client, there is no need for synchronize method. But, if i remove "synchronize" from all the methods, my Data class is not threadsafe, if single instance of Data is shared among all clients.


    Aren't those two sentences mutually exclusive?
    Assignment instruction says "Because multiple concurrent connections may exist, you must make both your server and the suncertify.db classes threadsafe." Does it mean i must make my Data class thread safe, though i am providing unique Data object per GUI client ?

    There must be some point where your threads will interact with each other. At a bare minimum I would suspect locking and reading/writing the file. All points of interaction are going to have to be threadsafe.
    Regards, Andrew


    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    Akash Singh
    Ranch Hand

    Joined: Aug 21, 2003
    Posts: 80
    Thank You Very Much. Your answers are always very helpful for me. I was very busy at work. This weekend i am planning to give some time for FBN assignment.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: FBN : RAF handle and Synchronize methods