aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock() and bookSeats() - - - - - - waiting for response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock() and bookSeats() - - - - - - waiting for response" Watch "lock() and bookSeats() - - - - - - waiting for response" New topic
Author

lock() and bookSeats() - - - - - - waiting for response

Bhuvan mehra
Ranch Hand

Joined: Mar 29, 2002
Posts: 69
i am not getting where i will modify() the record to bookSeats? a call in lock() method? if yes, to prpare new DataInfo, how i will get noOfseats to reserve.
if no, is that mean i have to perform only locking in lock().
and when bookSeats(int rn, int seats) method will be called, it will execute
{
---lock();
---read();
---modify();
---write();
---unlock();
}
[ April 10, 2002: Message edited by: Bhuvan ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

a call in lock() method?

No
if no, is that mean i have to perform only locking in lock().
and when bookSeats(int rn, int seats) method will be called, it will execute
{
---lock();
---read();
---modify();
---write();
---unlock();
}


Yes.
Mark


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

Joined: Mar 29, 2002
Posts: 69
hi Mark,
thanks for your response.
pls. clear one more thing. i hope lock()/unlock() impl. is correct. this impl. looks very simple.
private Map tmLockRecord = Collections.synchronizedMap(new TreeMap());
public void lock(int recordNo, VMID vmid) throws IOException
{
if(recordNo > 0)
synchronized(tmLockRecord)
{
try
{
while(tmLockRecord.containsKey(new Integer(recordNo))
wait();
tmLockRecord.put(new Integer(recordNo),vmid);
}
catch(InterruptedException iE)
{
iE.printStackTrace();
}
}
else if(recordNo == -1)
synchronized(tmLockRecord)
{
for(int i=1; i<= recordCount; ++i)
try
{
while(tmLockRecord.containsKey(new Integer(i))
wait();
tmLockRecord.put(new Integer(i),vmid);
}
catch(InterruptedException iE)
{
iE.printStackTrace();
}
}
}

PrintWriter printWriter = new PrintWriter(System.out, true);
public void unlock(int recordNo, VMID vmid)
{
synchronized(tmLockRecord)
{
if(tmLockRecord.containsKey(tmLockRecord))
{
VMID id = (VMID) tmLockRecord.get(new Integer(recordNo));
if(id.equals(vmid))
{
tmLockRecord.remove(new Integer(recordNo));
notifyAll();
}
else
printWriter.println("Someone trying to use unlock without permission. Id is : " + vmid);
}
}
}
Mark, your suggestions are always helpfull.
thanks and regards,
bhuvan.
[ April 10, 2002: Message edited by: Bhuvan ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

For the most part it is that simple. You handle the problem of client unlocking a record that they didn't lock, you have the Map which holds all the locked records with a VMID object. So that also tells me you are using the VM Id as the unique ID for the client.
I didn't change the lock method signature to include a client ID in my submission, so those parts of your code, I really can't comment on in a confident manner.
Is Map one of the new Collections objects, I forget. I used a HashSet, but I didn't need an Object to lookup. I just put in Integer classes. There is no Mapping in a HashSet, so again I can't really comment on that.
And also to the Collections.synchronizedMap() method, and then to have the synchronized block aren't one in the same. But I am not positive there either.
Hope that helps in some way.
Mark
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

For the most part it is that simple. You handle the problem of client unlocking a record that they didn't lock, you have the Map which holds all the locked records with a VMID object. So that also tells me you are using the VM Id as the unique ID for the client.
I didn't change the lock method signature to include a client ID in my submission, so those parts of your code, I really can't comment on in a confident manner.
Is Map one of the new Collections objects, I forget. I used a HashSet, but I didn't need an Object to lookup. I just put in Integer classes. There is no Mapping in a HashSet, so again I can't really comment on that.
And also to the Collections.synchronizedMap() method, and then to have the synchronized block aren't one in the same. But I am not positive there either.
Hope that helps in some way.
Mark
Bhuvan mehra
Ranch Hand

Joined: Mar 29, 2002
Posts: 69
Mark,
as you said you didn't use any id. did you use LockManager or any other tech. to verify the client?
pls. give a look on lock() code again and ignore vmid part and choice of collection API.

this part is for 30 marks. i hope, i am not missing any thing.
-------------------------------
And also to the Collections.synchronizedMap() method, and then to have the synchronized block aren't one in the same. But I am not positive there either.
--------------------------------
you are right. Collections.synchronizedMap() makes a collection threadsafe.
i took it like, if i have in built threadsafe collection API, still i need my block to be syn.
i hope i am not wrong.
----------------------------------------
but I didn't need an Object to lookup. I just put in Integer classes.
----------------------------------------
that was a typing mistake.
and pls. point out mistakes.
regards,
bhuvan.
[ April 10, 2002: Message edited by: Bhuvan ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Yes it looks fine.
I still have this Spider sense about the Synchronization point. I personally would rather see the synronize(Map){} than calling the Collections.synchronizedMap() method. But that's just me.
I did not use a LockManager, and the way I handled clients and which can unlock only ht e reocrds they locked is through the Connection Object. The Connection Object also had it's own HashSet of locked records, but it held only those by that specific client.
So when you called unlock on the ConnectionObject, it would search the HashSet, if the record was there, it would allow it to pass the call to the Data class, otherwise it threw an Exception.
Mark
Bhuvan mehra
Ranch Hand

Joined: Mar 29, 2002
Posts: 69
hi Mark,
i did changes at three places.
1.use TreeMap tmLockRecord= new TreeMap();
instead of Collection.Synchronize().
i was not able to convince myself that i can justify the step to examinar but was waiting for your response.
2. insteadof wait(), tmLockRecord.wait(); in lock()
3. insteadof notifyAll(), tmLockRecord.notifyALL() in unlock()
Last point, i have no details of connection object and to verify the client i have only one tech. that is change the sign. (pass identification of caller).
you have said so many times that we should not change sign. of lock()/unlock() with the reason(i have read in other threads).
either i can verify the caller or i can keep sign. as it is.
pls. take a decision for me.
thanks.
regards,
bhuvan.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Bhuvan:
1.use TreeMap tmLockRecord= new TreeMap();
instead of Collection.Synchronize().
Fine idea. But why use a TreeMap? You're don't seem to use the sorting properties of TreeMap (as you assign it to a Map, not a SortedMap, variable) and a TreeMap is much less efficient than a HashMap is.
Last point, i have no details of connection object and to verify the client i have only one tech. that is change the sign. (pass identification of caller).
I've been reading this forum on and off for ages, moderated it for half a year or so, and can tell you this:
  • People who used a Connection object or a client-side facade so they could keep track of client identity without changing the lock()/unlock() signature have passed.
  • People who changed the lock()/unlock() signature, and explained why they did it, have passed.
  • People who just thought it was all getting too messy and/or difficult and simply junked the requirement that a duplicate call to lock() should be handled have, to my surprise, passed.
  • Bottom line is: make a choice. Argue why it's a good one. Pass. Sun isn't interested in whether you made the very best design choices. It wants you to demonstrate that you can go through a decent and structured design and development process, document what you are doing, and end up with a functional product and maintainable code. In Real Life, modifying requirements in view of technical feasibility considerations is part of the game.
    pls. take a decision for me.
    No. You take the decision. That's what the assignment is all about.
    The only decision anyone should take for you is that you really ought to change your display name. Have a look at our naming policy and update your display name. Thanks
    - Peter
    [ April 11, 2002: Message edited by: Peter den Haan ]
    Bhuvan mehra
    Ranch Hand

    Joined: Mar 29, 2002
    Posts: 69
    Hi Peter,
    I visited your homepage and your resume. I want to ask a ques., I hope you will not mind to ans. ques which is not related to assign.
    1. when industry has people who have qualification like yours, need people like me who are not able to do the simple assign. without help like I am doing?
    regards,
    bhuvan.
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Bhuvan, three things. First, I too am a mere human being, I just didn't put any of the things I am crap at in my resume . Second, five years ago I knew a little bit about how to program but nothing about software development; two-and-a-half years ago I knew nothing about Java. You, too, will travel that road, even if it seems impossibly far right now. Hey, everything's relative -- I'm looking at the likes of Kyle Brown who moderates the Web Services forum and find myself thinking much the same things that you are thinking! Third, I am nowhere without a team, and in my team there's place for anyone willing to work hard and use the gray matter (not that we're in a position to hire right now, but that's not the point).
    Continue with your Developer's exam. Don't do it just to pass -- extract every last drop of knowledge and skills from it. Keep on questioning. After the exam, celebrate, relax, then take on the next challenge.
    If you'd like to respond to this, a private e-mail to pdh@javaranch.com is probably the way to go.
    - Peter
    [ April 12, 2002: Message edited by: Peter den Haan ]
    Cathy Young
    Ranch Hand

    Joined: Mar 02, 2002
    Posts: 126
    Originally posted by Mark Spritzler:
    I did not use a LockManager, and the way I handled clients and which can unlock only ht e reocrds they locked is through the Connection Object. The Connection Object also had it's own HashSet of locked records, but it held only those by that specific client.
    So when you called unlock on the ConnectionObject, it would search the HashSet, if the record was there, it would allow it to pass the call to the Data class, otherwise it threw an Exception.
    Mark

    Hi Mark,
    Which Connection? Are you talking about java.sql.Connection interface? How to use its HashSet, then?
    Thanks,
    Cathy
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    No, we're talking about our own Connection objects. Connection is a remote implementation of the Data interface. We give each client its own RMI-enabled Connection to talk to and use that as a way to identify different clients.
    - Peter
    Mark Spritzler
    ranger
    Sheriff

    Joined: Feb 05, 2001
    Posts: 17257
        
        6

    See now I'm a ventriloquist. However, I'd much rather let Peter speak in his own voice, because it is so well spoken, and he is much funnier than I am.
    Mark
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Now that's a double-edged remark if I ever saw one... :roll: tssk.
    - Peter (who can't help but identify with the first mouse)
    [ April 12, 2002: Message edited by: Peter den Haan ]
    Mark Spritzler
    ranger
    Sheriff

    Joined: Feb 05, 2001
    Posts: 17257
        
        6

    double-edged remark

    It wasn't meant to be.
    So does that then make it a triple-edged remark?
    I identify with the guy in the statds who watches the early bird, and the two mice as they have a round-robin Ping Pong match. No, that was just a dream I had last night. OOPS, sorry.
    Mark
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Originally posted by Mark Spritzler:
    It wasn't meant to be.
    I know
    - Peter
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: lock() and bookSeats() - - - - - - waiting for response