• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Locking Question

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark.
That does clear up things.
Regards
Suchak Jani
 
Suchak Jani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Enrico. Good explanation.
Mark
 
Suchak Jani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic