aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes the question of the lockmanager 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 "the question of the lockmanager" Watch "the question of the lockmanager" New topic
Author

the question of the lockmanager

peng chen
Ranch Hand

Joined: Jun 09, 2003
Posts: 35
hi ,everyone
I use the MVC to design the assignment ,but I encounter the lock problem.
I create a class named RemoteModel ,by it I use the method of the data to update and delete and so on .And in lock method of the data ,I use wait(),
and in unlock method of the data ,I use notify(),
In lockmanager I use the lock and unlock method of data class,and create a HashMap(recNo,RemoteModel)
to test the identify of the client .
In my class RemoteModel ,I create an instance of the lockManager .In the modify method of the RemoteModel I will write it follow as:
lockManager.lock(recNo,this);
data.delete();(data.update())
lockManager.unlock(recNo,this);
So if my design is availiable?
I am very hard to test the function of the lock.becaues my assignment isn't flight,it's house service.
[ June 30, 2003: Message edited by: peng chen ]

SCJP 1.4 SCJD SCWCD1.4 SCBCD1.3<br />SCEA OCP(8i%9i)
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Hi Peng
Welcome to JavaRanch.
You said your RemoteModel's modify() method will be calling Data's delete() method. This does not sound correct to me. Why are you calling delete()?
Do you create a new instance of RemoteModel for each client that connects? If so, then your plan to use the instance of the RemoteModel to identify the owner of the lock should work.
Testing should be easy. Temporarily put a delay into your RemoteModel's modify() method, between the call to lock() and the call to Data.update(). Then create two threads that will both try and modify the same record at the same time. Then see what has happened at the end of the run.
The reason for putting the delay into the RemoteModel's modify() method is so that you can guarantee that two threads are both trying to do the modify() on the same record at the same time.
For Fly by Night Services, each record had a counter which could be decremented many times. So we ran the test many times until the counter reached zero, at which point one of the threads would report that it was unable to modify the record.
For the hotel assignment, you can only book an individual record once. So I would expect that for the test above, one of the threads would book the record, and the other one would report that it could not book the record.
I do not know the details of the contractor's assignment, so I do not know what results you should expect. But you should be able to determine this yourself.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
peng chen
Ranch Hand

Joined: Jun 09, 2003
Posts: 35
thanks everyone
lockManager.lock(recNo,this);
data.delete();(data.update())
lockManager.unlock(recNo,this);
the data.update() should be just replaced data.deleted();
How can I delay between the lock and the update(),I try between them add the wait(),and add the loop,but it will make the gui trun out of control.
beacause when I preess the delete button ,I will used the RemoteModel to execute the db.delete();but when I add the loop or wait() ,the actionListener in the delete button will not accomplish ,then make the System out of control.

I has not created a new instance of RemoteModel for each client that connects ,I only
"new" a RemoteModelImpl,then give the instance to the interface (RemoteModel),the interface is extends the Remote ,every client at the client side bind the interface to get the actions to execute the modification to the db. In fact the
Model instace is created only one time when the Server is start.So the distinguish by the instance can't be aviliable.

And the second question is when the client is notified ,do he want to lock the num again in orde that the other who wants to modiry the same num must go in wait() when he is modifying .

the third question is :the unlock() is data class, when it use the notify() to notify others,
how can it knows who should be notify,I assume that one is lock(num=3),the other is lock(num 4),both them are wait;the unlock is just (num=3);

So I hava a idea is that when some one locks the num,others to lock the same num should be in wait(),but now I design that others the different num
also to wait(),at this time when the lockowner unlock the num,I use the notifyall(),let all in the wait() wake up to run.

That is to say :At any time I only permit one client to access the db.Is the idea aviliable?
[ July 01, 2003: Message edited by: peng chen ]
[ July 01, 2003: Message edited by: peng chen ]
[ July 01, 2003: Message edited by: peng chen ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Hi Peng,
Do your instructions require you to allow clients to perform deletes? I think you have to implement create() and delete() on the server side, but not on the client side.
How can I delay between the lock and the update()

All you need to do is call Thread.sleep() for a few seconds between your call to lock() and your call to <whatever modification method>.
If you do this, and you have two separate threads trying to call your modify() method for the same record at the same time, then because of the sleep, you should be able to have one thread blocked by the lock while the other thread is completing it's modification (it would be good to throw some debugging or logging messages into your server code so you can confirm that one thread did get blocked). Then, after the first thread has completed it's modification, the second thread should get a chance to run it's modification ... what happens then is up to your requirements - if each record can only be modified only once, then the second thread should fail to perform the modification with a suitable error message.
Just to make sure you understand: the delay is only for while you are testing your locking: when you are happy with your locking, you can (and should) remove the delay.
when I add the loop or wait() ,the actionListener in the delete button will not accomplish ,then make the System out of control.

This is a separate issue to the one you originally raised. This issue could affect you even without the built in delay that you are currently using. I don't think you need to worry about it for the assignment (but you would have to in real life). I dont know if you want to discuss it :
  • now,
  • or after you get your locking working the way you want it to work
  • [list]or never

    So the distinguish by the instance can't be aviliable.

    What is the this that you are using in the calls to lock() and unlock() then? If I understand you, it will not be unique per client, so I don't think it is serving any purpose.
    And the second question is when the client is notified ,do he want to lock the num again in orde that the other who wants to modiry the same num must go in wait() when he is modifying .

    Yes, upon notification, the client should attempt to get the lock.
    the third question is :the unlock() is data class, when it use the notify() to notify others,
    how can it knows who should be notify,I assume that one is lock(num=3),the other is lock(num 4),both them are wait;the unlock is just (num=3);

    Perhaps you should look at the notifyAll method then? Calling it will notify all waiting threads, and they can verify whether their lock is now available or not.
    Something else to think about: the UrlyBird instructions have the requirement: If the specified record is already locked by a different client, the current thread gives up the CPU and consumes no CPU cycles until the record is unlocked. If you don't have this requirement, then you are safe with what I said before. If you do, then strictly speaking, I don't think you can meet this requirement by having a common object on which all client's call wait() and notifyAll(), because if you do this, then a client will consume CPU cycles anytime any lock is released, not just the lock the client is interested in. I won't go into that any further unless asked.
    So I hava a idea is that when some one locks the num,others to lock the same num should be in wait(),but now I design that others the different num
    also to wait(),at this time when the lockowner unlock the num,I use the notifyall(),let all in the wait() wake up to run.
    That is to say :At any time I only permit one client to access the db.Is the idea aviliable?

    I think that this will work since you can only do an update by calling your modify() method which ensures that the operation should be fast.
    However I personally don't think this is a good design, because it will not cope well with growth, especially as more and more user's start using the system.
    Regards, Andrew
    peng chen
    Ranch Hand

    Joined: Jun 09, 2003
    Posts: 35
    Hi Andrew and others .I has resolved the locking
    problem
    (I don't use the RemoteData ,but use the Date in
    RemoteMode.is this availiable.

    In server side I set the only lockmanager in it and let all data to visit the lockmanager.By lockmanager ,the data class konws if the recNo is locked.so the lockmanager is the control all locks.the programme can run good.

    Thanks Andrew I want to ask:if all class params
    will write comment.and all the classes will set to one jar called runnme.jar(MUST not call others)
    but out of the runnme.jar also is jar,bur is the jar's name can be called by myself.
    Andrew ,I want to mail the assignment eo you ,
    Please give the mail address to me .or the QQ number
    [ July 17, 2003: Message edited by: peng chen ]
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11481
        
      94

    Hi Peng
    if all class params will write comment

    Are you asking if you need to write JavaDoc comments for all parameters of all your methods of all your classes? Yes you do. You also have to write JavaDoc comments for all the class level variables within your classes.
    and all the classes will set to one jar called runnme.jar(MUST not call others) but out of the runnme.jar also is jar,bur is the jar's name can be called by myself.

    I am not clear on what you are trying to do here. Are you trying to have additional jar files inside the runme.jar file?
    If so, I don't see any value in this - the examiner will not unpack the runme.jar file, and they expect to only have the runme.jar file for the class files ... no others.
    Regards, Andrew
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: the question of the lockmanager