Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized

 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not clear about method synchronization. Here is an example:
public class CubbyHole {
private int contents;
public synchronized int get() {
...
}
}
My question:
1. when method get is called, it locks only the thread instance that called it, or locks all thread instances of the same type?
The SUN webpage says:
" method declaration for put contains the synchronized keyword. Hence, the system associates a unique lock with EVERY instance of CubbyHole ... ..."
http://java.sun.com/docs/books/tutorial/essential/threads/monitors.html
2. If method synchronization locks EVERY instance, it implies a database GLOBAL lock in this assignment, as long as the program access the database from single class like Data. Remeber, the instruction says "at most one program is accessing the database file". So, no dirty read here.
Any comments?
Shan
 
Michael Dreese
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shan chen:
the instruction says "at most one program is accessing the database file". So, no dirty read here.

Yep, we don't have to worry about dirty read.
Remember that synchronized method will be treated as an atomic statement.
Cheers,
Michael
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shan,
the instruction says "at most one program is accessing the database file"

I dont have anything like this in my assignment - is this the new assignment?
The system associates a unique lock with EVERY instance of ...

Just to be clear here (or attempt to anyway ). The system associates a unique lock with each and every instance of ....
So, using the CubbyHole example:

ch1 has a unique lock, as does ch2, as does ch3. This is 3 locks, not 1 unique lock against all instances.
So when you call ch1.put() c1's lock is invoked, and no other thread can call ch1.get(). However ch2 has a seperate unique lock, so any thread could call ch2.get() or ch2.put().
Regards, Andrew
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Andrew,
My assignment is new assignment. I got it at April 4th.
Thanks for clearing my concept.
 
shan chen
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a further question about RMI regrading to multiple instances. In the following code block, DatabaseImpl extends UnicastRemoteObject.
java.rmi.registry.LocateRegistry.createRegistry(1099);
DatabaseImpl dImpl = new DatabaseImpl();
Naming.rebind("Mediator", dImpl);
My question: will dImpl be the only instance of DatabaseImpl on the server side through server life, or a new instance of DatabaseImpl will be created for each request?
Why I asked this question: DatabaseImpl includes class Database which implements all db operation. If there is only one DatabaseImpl instance, then there is only on Database instance, and then a synchronized method in Database will guarate a GLOBAL lock.
Thanks for any suggestion.
shan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic