• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronized

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic