File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Locking Question 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 "Locking Question" Watch "Locking Question" New topic
Author

Locking Question

Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6


Mark,
When one has lock(int) and unlock(int), in the lockmanager utility class, then does one not also store all the locked record numbers in a collection type variable in the lockmanager ?
With the above being true what happens to :-
1. lock(int) and unlock(int) methods asked to be implemented in Data by the assignment instructions.
2. the lock all (-1) records function.
Do feel free to point me to some discussion thread if that's where you think the answer lies.
Thanks for all the help.
Regards
Suchak Jani


good question. Unfortunately I did not have the LockManager class. I should have though. I believe it should have all the locks of all the clients. And each clients Remote object has a set of the locks that that client has, and no other client.
1. Then the Data class does not need to implement lock or unlcok, just leave it the way it is there.
2. and -1 loops thorugh the records and locks them all in the LockManager via a "Server" client.
something like that sounds good.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Suchak Jani
Ranch Hand

Joined: Jan 24, 2002
Posts: 68
Thanks Mark.
That does clear up things.
Regards
Suchak Jani
Suchak Jani
Ranch Hand

Joined: Jan 24, 2002
Posts: 68
What do you think of this Mark ?
public synchronized void lock(int record) {
if(lockallrecords == record){

// get recordcount form the single instance of Data
for (int k = 1; k <= recordcount ; k++){
while((locks.contains(new Integer(k))))
{

try{
wait();
}catch(InterruptedException ie){
///
}
}

locks.add(new Integer(k));

}


}else{
while((locks.contains(new Integer(record))))
{

try{
wait();
}catch(InterruptedException ie){
///
}
}

locks.add(new Integer(record));

}
}
}
public synchronized void unlock(int record) {
if(locks.contains(new Integer(record)))
{
locks.remove(new Integer(record));
notifyAll();
}

}
}
Regards
Suchak Jani
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Looks good. I actually used a synchronized block instead of the lock method being synchronized. meaning
public void lock(int record){
synchronize(lockedRecords){
blah blah blah
}
Something like that.
I am confusing myself. But does the synchronize block only lock the lockedRecords object, or does it lock the Data object like synchronizing the method?
Anyone?
Mark
Suchak Jani
Ranch Hand

Joined: Jan 24, 2002
Posts: 68
Mark,
I went to borders to buy a book on threads yesterday night before writing the above code , because i had the same question. Could not get a decent book at a decent price but at least i glanced through some of them and here is what i got out of them.
Let's suppose i have the following code in a LockManager class(instead of Data class).
public void lock(int record){
synchronize(lockedRecords){
blah blah blah
}

The above will lock the lockedRecords object and thus the wait and notify will be : --
lockedRecords.wait() and lockedRecords.notifyAll().
But the above does not prevent another thread entering the LockManager code even though there is the synchronize block as the block does not lock the LockManager code.
So i thought that since the aim is to lock LockManager code , then i guess the best is to do synchronize at the method so that it will lock the LockManager code.
Lastly synchronize on the method is synchronize(this) and that locks only that particular instance of LockManager . But that does not matter as i have LockManager as a singleton.
The above is my understanding , it may be flawed and thus i would gladly welcome any comments/questions/critisizms.
Thnaks and Regards
Suchak Jani
Reshma Das
Ranch Hand

Joined: Jan 29, 2002
Posts: 125
Iam going mad on syncronized stmt. i have a code like
class textlock{
public syncronized reserver(){
System.out.println("lock called");
data.lock();
System.out.println("bookcalled");
data.book();
System.out.println("updatememorycalled");
data.updatememory();
System.out.println("unlock called");
data.unlock();
}
} //end of class
Data{
lock(){
syncronized(arraylist){}
}
unlock(){
syncronized(arraylist){}
}
syncronized book() {}
synccronized updatememory(){}
}
then i wrote a seperate class which implements Runnable interface . in it constructor, it runs a for loop
for(i=0;i<30..)
testlock.reserver(fltno,tickets);
the output is so horrible. can someone explain why iam getting non syncronized outputs. all my methods are syncronized. iam so confused.

output:
lock called
bookSeats called
26 pendingseats= 8 leftoutseats = 7
lock called
bookSeats called
30 pendingseats= 8 leftoutseats = 7
lock called
bookSeats called
31 pendingseats= 8 leftoutseats = 7
lock called
bookSeats called
updateMemory called
1 pendingseats= 7 leftoutseats = 6
lock called
bookSeats called
2 pendingseats= 7 leftoutseats = 6
lock called
bookSeats called
updateMemory called
updateMemory called
3 pendingseats= 7 leftoutseats = 6
lock called
bookSeats called
updateMemory called
4 pendingseats= 7 leftoutseats = 6
Enrico Mannarino
Ranch Hand

Joined: Dec 14, 2001
Posts: 133
Hi folks,
Question from Mark: “But does the synchronize block only lock the lockedRecords object, or does it lock the Data object like synchronizing the method?”
My solution is the same as Marks. The synchronize block only locks the lockedRecords object, not the Data object! Important! The modify method is synchronized itself, so two different threads can not use it at the same time. That method would have been enough, if you didn’t have to check no of seats available.
The lock mechanism just ensures you that 2 different users can not continue the modify process at the same time.
My code is something like this:
Lock
Read
CheckSeatsAvailable
Modify
Unlock
So if 2 different users try to update the same flight the second will wait at lock until the first thread have finished; i.e. passed unlock.
Hope it was clear!
Enrico
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Thanks Enrico. Good explanation.
Mark
Suchak Jani
Ranch Hand

Joined: Jan 24, 2002
Posts: 68
Enrico/Mark
What if lock and unLock are not in the Data class and are a part of another class (LockManager), would then, there be any harm/advantage in syncronizing on the locking methods.

Reagards
Suchak Jani
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

would then, there be any harm/advantage in syncronizing on the locking methods.

I can't really answer that as I didn't have the LockManager class in my submission. Though I think it is a better design and should have doen it that way .

Mark
Rajesh Matti
Ranch Hand

Joined: Jan 07, 2002
Posts: 121
There are 3 levels of synchronization.
1. Class : Every class has its own monitor(semaphore), meaning that all static methods, if synchronized, will use the class monitor(ONLY one per class).
2. Instance: Evenry instance of a class will have its own monitor, all messages sent by different therads to THIS OBJECT, will have to be serialized via this object's monitor.
3. Block : Block synchronization must include a object (which is not null !!!) hence, the monitor of the object will be used.
2 & 3 will have serialized access to the resource if invoked on the same object.
[ February 12, 2002: Message edited by: Rajesh Matti ]
[ February 12, 2002: Message edited by: Rajesh Matti ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Locking Question