aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Locking 101,  and Max's book 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 "NX: Locking 101,  and Max Watch "NX: Locking 101,  and Max New topic
Author

NX: Locking 101, and Max's book

Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
When synchronizing a method in a class, you restrict other objects
from calling other synchronized methods on the class, but is this at
the class level or the instance level. In other words, if I create
a new Data instance for each instance of my client then does the synchronizing of Data's methods (update, delete...etc) become useless. Or does the synchronization apply at the "Class Level". In which since all of my Data classes are created on one JVM (on the server), then if you call
a synchronized method on Data you lock the other instances from calling
other synchronized methods since the lock is at the Class level and not
the instance level.
If it is the case that its pointless to synchronize you Data methods
(update, insert, delete...) because each client gets its own instance
of Data and the lock is at the instance level, then why does Max synchronize all of his methods in DVDDatabase - because we hands each client a new instance of DVDDatabase (equivalent to my Data class)?
Max covers this in his book but I got lost!@!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Hi Bill,
When synchronizing a method in a class, you restrict other objects
from calling other synchronized methods on the class, but is this at
the class level or the instance level.

If it is a static method, then it is at the class level.
If it is not a static method then it is synchronized on the instance.
why does Max synchronize all of his methods in DVDDatabase - because we hands each client a new instance of DVDDatabase (equivalent to my Data class)?

Does each client get a new instance of DVDDatabase?
I just did a quick look at the code, and it seems to me that RegDVDDatabase registers one instance of DVDDatabaseImpl which is then used by all connecting clients. I don't see a factory here.
Since there is only one instance of DVDDatabaseImpl, which has one instance of DVDDbAdapater which has one instance of DVDDatabase (try saying all that fast) then there synchronization will be fine.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Hi Bill,
When synchronizing a method in a class, you restrict other objects
from calling other synchronized methods on the class, but is this at
the class level or the instance level.

If it is a static method, then it is at the class level.
If it is not a static method then it is synchronized on the instance.
why does Max synchronize all of his methods in DVDDatabase - because we hands each client a new instance of DVDDatabase (equivalent to my Data class)?

Does each client get a new instance of DVDDatabase?
I just did a quick look at the code, and it seems to me that RegDVDDatabase registers one instance of DVDDatabaseImpl which is then used by all connecting clients. I don't see a factory here.
Since there is only one instance of DVDDatabaseImpl, which has one instance of DVDDbAdapater which has one instance of DVDDatabase (try saying all that fast) then there synchronization will be fine.
Regards, Andrew
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Hi Bill,
You know, it's fairly hard to know how much detail is 'enough' and how much is 'too much and confusing' when you write a book like this, so you pretty much end up winging it. But enough Apologia, here's your answer.
In general, when a synchronized method interacts with a memory structure, it gets a fresh copy of the structure, then holds the lock on that structure( if it's the locked resource). This prevents other Threads from modifying the locked structure, and guarantees that the synchronized method is 'seeing' the latest version of the data structure when it starts. You need this process to make sure that the structures being used are current. By synchronizing the methods on the data class, you are guaranteeing that they are, in fact, seeing the latest state of any structure they are working with when they start.
You can read more about it here
All best,
M
[ September 15, 2003: Message edited by: Max Habibi ]

Java Regular Expressions
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
thanks guys. This clears things up - for now anyway!!
Bill Robertson
Ranch Hand

Joined: Mar 21, 2003
Posts: 234
Actually I do have another question. What is the advantage of making
the synchronized methods static versus handing out one instance of
the class and not having the classes methods static? Is it all a matter
of design (locking is a tough subject...any books you recommend?)
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Bill Robertson:
Actually I do have another question. What is the advantage of making
the synchronized methods static versus handing out one instance of
the class and not having the classes methods static? Is it all a matter
of design

Pretty much. Static synchronized methods just lock on the Class.
[QB}
(locking is a tough subject...any books you recommend?)[/QB]

There are lot of really good Threading books out there, including Taming Java Threads by Holub, which is my favorite. However, if you really want to dig into, read into the spec: I think it's particularly well written.
M
 
 
subject: NX: Locking 101, and Max's book