Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DatabaseManager - design decision

 
Makeshkumar Subramaniam
Greenhorn
Posts: 21
Chrome Eclipse IDE 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.


 
Makeshkumar Subramaniam
Greenhorn
Posts: 21
Chrome Eclipse IDE 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 ?



 
Roel De Nijs
Sheriff
Posts: 10594
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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!
 
Makeshkumar Subramaniam
Greenhorn
Posts: 21
Chrome Eclipse IDE 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: 10594
143
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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?
 
Makeshkumar Subramaniam
Greenhorn
Posts: 21
Chrome Eclipse IDE 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic