Hi: I am using socket to implement the assignment. I asked a question about lock/unlock before, With member's help, I do it in the background on server side. But I am confused on how to notify client after unlock. Because I use socket, if the client wait for reply, code like this while( true ) { fromServer = in.readLine(); } the client have to do nothing until the result of booking ticket come back. How to implement it? Thanks. Paul
Hi Paul, The best way to avoid a busy wait, is to call wait() on the object and after the event that you are waiting on occurs call notifyAll(). It is quite simple to do this in Java using the Observer interface and by having the class that you are wait()ing for extend Observable. The Observable calls setChanged() and notifyObservers() when the event completes and in turn the update() method on the Observer is called. So in your update() method on the Observer you could call notifyAll() to release the thread that is waiting for the event to complete.
Hi Michael: Thank you for your reply. But the problem is my design is view -> model -> dbclient, I have used Observer pattern on view-model, the model class extends Observable, so that I can not use it on db client and model. And the db client is not a thread, when client need send data to server it will be called and work for it. So if client wait for the reply continuelly, user can do nothing at that time. Thanks. Paul
Hi, I think making the client wait till a response arrives from server is the best way to do it. Otherwise each request to server should be done in a seperate Thread. I think a single Threaded model is just fine. This is a common problem no matter the implementation is Sockets or RMI. In both the implementations, the call will wait till lock is acquired. I think this approach is quite OK. But dont know for certain since I am also doing the assignment and have not submitted yet. regards Jacob
Hi: I think client wait for reply may not be a only way but at least it is simple to implement. To image that you book tickets on web site, after push book button on a web page, you have to wait the response. Can you book an ticket or do other things at that time? I think you can not. ok, I make it wait until the reply arrive. Thank you for reply. You give me a great help. Paul
Hi Paul, All of my booking requests occurred on a separate thread. I also had a problem with Observer like you, so I implemented a "roll-your-own" Observable to overcome the problem. I just kept a Set of Observers and in my home grown notifyObservers() method, called update() on all the registered Observers. Running each booking operation in a separate thread overcomes the problem of a deceased client that may have owned the lock when it died. So long as you have provided a means to release those locks at some point. Hope this helps, Michael Morris
Post by:autobot
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, that will fit in a stocking