wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes DatabaseManager - design decision 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 "DatabaseManager - design decision" Watch "DatabaseManager - design decision" New topic
Author

DatabaseManager - design decision

Makeshkumar Subramaniam
Greenhorn

Joined: Aug 09, 2008
Posts: 21

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.



cheers,
Makesh.

[URLyBird1.1.1 - > just started]
Makeshkumar Subramaniam
Greenhorn

Joined: Aug 09, 2008
Posts: 21

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
Bartender

Joined: Jul 19, 2004
Posts: 5222
    
  12

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!


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Makeshkumar Subramaniam
Greenhorn

Joined: Aug 09, 2008
Posts: 21

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
Bartender

Joined: Jul 19, 2004
Posts: 5222
    
  12

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

Joined: Aug 09, 2008
Posts: 21

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DatabaseManager - design decision