Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Locking 101, and Max's book

 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys. This clears things up - for now anyway!!
 
Bill Robertson
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic