Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Peter den Haan:
Briefly --- You are using Object.wait() as if it were Thread.sleep()! Get rid of the timeout and use notifyAll() in the unlock method. Assuming this code is sitting inside some kind of lock manager class (recommended), why a synchronized block instead of a simple synchronized method? An integer is not a valid hash key. The way you are currently using the HashMap, it really wants to be a HashSet. But it will probably end up being a HashMap mapping record number to some type of client ID, right? Finally, unlock() needs synchronization and the abovementioned notifyAll().
- Peter
Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
That is not the reason. The reason is that if you don't synchronize unlock() the code is badly broken. If another thread is updating the HashMap the very moment unlock() is being called,Originally posted by Mark Spritzler:
The reason why you want to synchronize in the unlock method, is simply because that is what they are expecting to see.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Peter den Haan:
[B]Originally posted by Mark Spritzler:
If I understand your code correctly, there is a 1:1 relationship between the HashMap and the object that has the lock() and unlock() methods. Synchronizing the methods is therefore equivalent to blocks synchronizing on the HashMap, but a a lot clearer.
hi,Peter , it is very kind of you,thanks alot . But what exact you mean above words? would you please explain it to me in detail ? also ,can i the modify lock/unlock signature ? you know , there is no synchronized modifier in lock/unlock assigment . BTW , if We abtain the lock of the object that has the lock() and unlock()method, does it mean we lock the whole DB ?
Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
It stops other threads from calling any synchronized method on the object. It works on a per-object basis, not a per-method basis. Actually it is equivalent to a synchronized(this) block.Originally posted by Mark Spritzler:
I believe that when a method is synchronized, only one thread can call that method at a time, but it doesn't stop other threds from calling other methods on that class.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Heh. You just hit one of my hobby horses - yes, you use a single instance of Data, just beware of imposing a Singleton pattern on it. Remember, you're coding for reuse, and the next project that comes along might have a need for multiple tables.Originally posted by Martin Habicht:
but im my design I use a single instance of Data, which is shared by all clients. [...] Or just say Data is a Singleton.
When it comes to monitor locks there's nothing you need to do apart from sticking the word "synchronized" in a few places. But sometimes the thread gets its monitor lock, goes into a synchronized method and then discovers that some condition isn't met. For instance, the method in question is lock() and another client already owns the lock in question. That's where wait()/notify() comes in.a) do I need to call wait() and notifyAll() in a loop or doesen't it already works in a way, that thread_2 waits automatically until he can get the monitor?
Record locks work a bit like the monitor lock, but on a completely different level. They lock records, not objects. They live across machines and method calls and aren't tied up with code structure like a synchronized block is; instead, you call lock() and unlock() methods. You need locks to prevent race conditions, i.e. multiple clients from booking the same seat at the same time.b) what for do I need locks in this situation? (apart from the sun requirement)
Yes. A synchronized method is slightly more efficient because there's a special bytecode for it.PS: did you specialists knew that synchronize(this){method body} and syncronized method() and has the same effect, but gets compiled to different bytecode? But that's a different issue...
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Peter den Haan:
Originally posted by Mark Spritzler:
[b]If every instance of the class has its own copy of the collection (this is what I meant with a 1:1 relationship), it is not necessary to synchronize on the collection; just use synchronized methods, they are easier to take in.
- Peter
Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Peter den Haan:
Dennis: Given that you have only one copy of everything (collection, db class, server class) it doesn't matter what you synchronize on - as long as everything synchronizes on the same thingSynchronized methods are easier to read though IMHO.
The data structure you've chosen to contain locks (Vector) might be inappropriate though. Not only does it needlessly add its own synchronization, the contains() and remove() methods have O(n) performance (where n is the number of outstanding locks), which is only acceptable if you are optimistic about the number of locks outstanding at any given time. For FBN that optimism is fine, but you are asked to code for re-use. Replace the Vector by a HashSet, which lacks synchronization, has O(1) performance for all relevant operations, and is a better expression of the kind of collection your locks are (mathematically speaking, they really are a set and certainly not a list; in plain English, you can only hand out one copy of each lock and you don't care a hoot about the order of the locks.)
You've got some duplicate information: a database lock is both indicated by DBLock and a -1 record lock; can't you eliminate one of them? The database lock code is broken, I think - it looks like no database lock will be set unless there are other outstanding locks, or am I misreading the code? The & wants to be && also.
Finally, pull your "new Integer()" statements out of the loop, no need to create new objects every time someone calls notifyAll().
- Peter
Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Originally posted by Peter den Haan:
Dennis, looks good (I think: so test!) just one very minor point:
- Peter
Sun Certificated JAVA Programmer(SCJP)<br />Sun Certificated JAVA Developer(SCJD)<br />Sun Certified Web Component Developer(SCWCD)
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Mmmm... it doesn't say it has to be implemented in Data itself.Originally posted by Noor, Omar:
according to the assignment requirement, lock/unlock has to be implemented as part of Data API.
See here for some hints. There's plenty of discussion if you trail through past threads too.To determine [client ID], using currentThread reference, I think, is not good idea because of RMI or if the server wants to free resources of dead client thread. Given that requirements, how unlock() with only the record number parameter will know who is unlocking this record??
True, and it was touched upon twice earlier in this thread. However, experience shows that you can pass without satisfying this requirement, and Dennis is rather pressed for time.As Dennis code demonstrates this [...] I think that does no fulfill the requirement!!
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Your point is a valid one. However, seeing that the lock() and unlock() code can be expected to hold locks only very briefly, don't expect any great gains in concurrency. And if your design is clean and you implement your locking code in a separate class, the synchronization will be properly factored out without you really having to think about it.Originally posted by Rasika Chitnis:
For example, while one thread is excuting lock method, no other thread will be able to search flights because getRecord method (which is synchronized) can not be executed. I do not think we want this to happen. [...] I think there is a benefit of having synchronized block instead of synchronized method.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Apologies for being unclear. The lock file would certainly not be the database file! The problem we're trying to solve is, we need a foolproof way of claiming ownership of the database file even if two different client processes are started simultaneously. One popular way of doing this, and just about the only reasonable way that Java gives you access to, is using lock files. To claim ownership of a file (or set of files), say "db.db", you create an accompanying lock file, say "db.lck". If you succeed, you own the database. If the lock file already existed, you don't. The atomicity of createNewFile() is important, it means that you cannot get race conditions when two clients try to claim the database simultaneously. As long as these clients agree on the lock file to useOriginally posted by Rasika Chitnis:
[...] But we are not creating a new database file for each client in this assignment, so this is really not that useful in this case.
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
What conceivable reasons could there be why this would happen?Originally posted by Rasika Chitnis:
If for some reason unlock method can not unlock a record, should it throw an exception ?
Peter den Haan | peterdenhaan.com | quantum computing specialist, Objectivity Ltd
Bisi
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|