Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

DatabaseManager - design decision

 
Greenhorn
Posts: 21
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have created a worker class called DatabaseManager which does following:

1. managing recordCache map (instance variable)
2. managing randomAccessFile for read write (instance variable)
3. reading schema details
4. reading data into recordCahe when object is created for DatabaseManager

is this design okay? or i should delegate the randomAcessFile stuffs to a new class ?

And I have some questions on (nested?) synchronization. please advise me if following code would create problem in multi-threading? :


also,



Thanks,
Makesh.


 
Makesh Subra
Greenhorn
Posts: 21
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
I think the above scenario is okay. I'v written following code and tested it.




Now, my question is : coding like this (nested synch) would have any impact in the score ?



 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Makeshkumar Subramaniam wrote:I think the above scenario is okay.


That's wrong, you will certainly risk a deadlock! And as a second (very important) remark: a real thread is started with its start-method, not with run-method!

Take a look at this scenario: when threadA gets the lock on the cache-object and then it moves back to the runnable state, threadB starts running and gets the lock on lock-object and then threadB moves back to runnable state. And this scenario is simple proven with following code


Output:
cache start
lock start

And the program will never end!
 
Makesh Subra
Greenhorn
Posts: 21
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:
And as a second (very important) remark: a real thread is started with its start-method, not with run-method!


oops. good catch. Thank you. i was bit sleepy last night

Roel De Nijs wrote:
Take a look at this scenario: when threadA gets the lock on the cache-object and then it moves back to the runnable state, threadB starts running and gets the lock on lock-object and then threadB moves back to runnable state. And this scenario is simple proven with following code



Okay. I think I got

basically when the DatabaseManger initializes, I just wanted to do read all record from disk and put them into cache.


and I've some methods writing things from cache to disk.




so this scenario, I assume, exactly matches with your code...

Now that I'll have to think over a different approach then !

Thanks for your time !
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to make sure that you always synchronize on objects in the same order.

So when methodA has

then methodB should also have

and the program will end normally, without deadlock

But if you have such nested synchronized blocks, why not synchronize the complete methods?
 
Makesh Subra
Greenhorn
Posts: 21
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

But if you have such nested synchronized blocks, why not synchronize the complete methods?



Thats what exactly I've done...
also have executed the Data Class testing program found here. (with almost 5000 threads ! )

It'd executed well without any problem (deadlock).


Thanks
 
I didn't say it. I'm just telling you what this tiny ad said.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic