aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes lock/unlock review Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "lock/unlock review" Watch "lock/unlock review" New topic
Author

lock/unlock review

Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Andrew Monkhouse:
I think the answer to that would be getting well outside of the scope of this assignement.
However the basic answer is the amount of space required for all these locks.
If you have 20 tables to lock, each with 100 records, you will be allocating space for 2,000 key / value pairs plus any class overhead.

Not sure I'm following you here Andrew: this method doesn't require that you keep track of unlocked records. Thus, with 2,000 potential locks, the actual amount of lock obtained at any one time would be, by defination, the minimal number needed. That is, it could be 1, or 10, or 2000. This is consistant with any other approach, I would think.
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4


Java Regular Expressions
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Rajesh Rajesh:
Hello Max,
I posted the above after testing for add() only. But your idea works perfectly fine for delete(), modify(), lock(), unlock() and all methods of Data (except add()) for 200+ multi threads. There was no errors.
The add() operation error is not because of Max's principle. I tried to add() from several threads and above error popped up. Although add() is not for assignement, its lack of support for multi threads persists. Will not solving add() reduce marks???

I don't believe so, as it didn't on mine, nor has it on anyone else's that I've seen. Quite frankly, given the state of the code, I don't think that add can be solved without modification, unless everyone is forced to go through the very same map, or the implementation of add is changed. For example, as I recall, delete didn't even decrement the record count.

If the question in essay exam asks how do you plan to upgrade Data for more than one database, the answer would be to have a static Map for every Database. Is this practice of having a number of static maps a good practice?
Rajesh

That would certainly work, though I had thought that the RequestBroker might actually create a map, and dole out references to that map for each client that wanted a reference to a given data base: this would act very much like a static map does now. Of course, then the Data class would need a setMap(map) method, and the lock /unlock methods would have to use that, but I don't think it would take more then a couple of hours to write that.
All best,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11483
    
  94

Hi Max,
It appears I may have missed what the original question was asking (about allocating 'x' maps), and gone off on a non usefull tangent.
Andrew:
If you have 20 tables to lock, each with 100 records, you will be allocating space for 2,000 key / value pairs plus any class overhead.
Max:
Not sure I'm following you here Andrew: this method doesn't require that you keep track of unlocked records. Thus, with 2,000 potential locks, the actual amount of lock obtained at
any one time would be, by defination, the minimal number needed. That is, it could be 1, or 10, or 2000. This is consistant with any other approach, I would think.

I think I went off into never never land there, so the rest of this can be ignored :-)
But what I was talking about is the memory requirements of the empty HashMaps. So a HashMap with an initial capicity of 5 and 2 locks allocated could use memory like:

(Actual implementation is very dependant on the implementers of course )
So even though there are only two locks being stored, the JVM has allocated space to track more locks. This is not very much space, but it does exist. Sorry, but I am going to have to revert to C for a moment here:

As the C code shows, because I have allocated space for 5 strings, I have used 20 bytes, even though they currently contain nothing.
Getting back to the Java: the same thing happens (albeit behind the scenes): When I ask to create a HashMap with an initial capacity of 5, it will allocate at least 5 sets of:
(from looking at my copy of the Java source).
Going back to my original comment, let's say that each object is stored as a 4 byte pointer, that is 16 bytes per Entry. If I have 2,000 entries I will have used 32Kb before I store my first lock.
That is what I was getting at, even if it is way outside what we should be thinking about (this is the sort of thing I was expected to think about at my last job which was a C coding position - sorry ).
Regards, Andrew
[ April 28, 2003: Message edited by: Andrew Monkhouse ]

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

Joined: Aug 03, 2002
Posts: 7729
If I have 2,000 entries I will have used 32Kb before I store my first lock.
How much does 512Mb memory cost?
Sorry, I've been dying to say this for ages ( I get it said to me all the time, comes from programming an 8K PDP8, I guess )


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11483
    
  94

Hi Barry,
How much does 512Mb memory cost?

Ouch - I knew someone would say that
But how much does the downtime for upgrading the ram cost?
And in the last place I worked at, having too large a memory footprint affected how quickly page faults could be processed, which affected how much throughput we could get out of some heavily loaded machines.
But we are way off topic, so I think I will stop this thread before someone yells at me
Regards, Andrew
Qusay Jaafar
Ranch Hand

Joined: May 06, 2002
Posts: 127
How did you pass mapLocks to Data class?
Regards


Qusay
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11483
    
  94

Hi Qusay,
You asked: How did you pass mapLocks to Data class?
I think this thread has been discussing having a LockManager class which handles the specifics of the locking for us. So the Data class never gets a copy of the mapLocks object.
Now, if your LockManager is a singleton, then you can either get a reference to it in the Data constructor, or you can get the reference to it and call a method on the reference within the Data.lock() and Data.unlock() methods.
If your LockManager is not a singleton, but instead contains a static mapsLock, then you can create a LockManager object in your Data constructor, and call its methods in the Data.lock() and Data.unlock() methods.
I know I have been vague - this is because I am unsure how you are looking at implementing locking. If have not given you enough to think about, then please try and make your question a bit more specific to how you would like to implement the lock, and we will see what we can do to help.
Regards, Andrew
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Andrew Monkhouse:
Hi Qusay,
You asked: How did you pass mapLocks to Data class?
I think this thread has been discussing having a LockManager class which handles the specifics of the locking for us. So the Data class never gets a copy of the mapLocks object.
Regards, Andrew

Well, there is another approach, and that is having a Map that is a static member variable of Data. IMO, that's the cleaner approach (though there are very bright and reasonable people who disagree), and the tendency of this thread, I think.
M
Perry Board
Greenhorn

Joined: May 03, 2003
Posts: 29
[Whew, this is a long thread. Thanks, everyone, for all this valuable discussion.]
Max, I like the idea of keeping the hashmap inside of the Data class. I think it makes sense that if the Sun interface has lock and unlock methods, and we need to do locking and unlocking, that we might as well do it in Data, rather than another place leaving empty code blocks in Data.
The part I'm not clear on, or am concerned about is that it looks like the idea is to use the data object itself as the value representing the client who has locked a record. And this requires that each client get a unique instance of Data, correct?
The more I think about it, the more I think it might be an effective solution. It just seems strange to instantiate an entirely new Data for every client--in other words, the sole purpose for multiple instances of Data is to use the object reference as an identifier, right? Or are there other reasons why each client would need a unique instance of Data? If I'm missing the point entirely, please let me know.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: lock/unlock review