• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

FBN : RAF handle and Synchronize methods

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    Akash Singh
    Ranch Hand
    Posts: 80
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    A wop bop a lu bob a womp bam boom. Tutti frutti ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic