Sie sind auf Seite 1von 6

6/13/13

1) no cpu cycles 2) book a selected record 3) the general user interface level (SCJD forum at JavaRanch)

Big Moose Saloon


A friendly place for programming greenhorns!
Search

Java FAQ

Recent Topics

Register / Login

A special promo: Enter your blog post or vote on a blogger to be featured in an upcoming Journal

JavaRanch Java Forums Certification Developer Certification (SCJD/OCMJD)

Author

1) no cpu cycles 2) book a selected record 3) the general user interface level
posted 10/1/2009 9:45:33 PM

Mika Tapanainen Ranch Hand Joined: Jun 11, 2009 Posts: 91

Hello and thanks for this excellent discussion group!! I have different B&S questions: 1) My interface has DB.lock method and the requirement is "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." I implemented the 2 phase locking, I have the MasterLock and the RecordLocks. The MasterLock is for the HashMap locking. The HashMap contains record numbers and the RecordLocks. The RecordLocks are for the locking of the records. Now no CPU cycles are consumed when the thread waits for the lock of the some record. Do you think so that 2 phase locking is really needed? I think my solution would

www.coderanch.com/t/464865/java-developer-SCJD/certification/cpu-cycles-book-selected-record

1/6

6/13/13

1) no cpu cycles 2) book a selected record 3) the general user interface level (SCJD forum at JavaRanch)

be simpler if I would have only one lock. I know that in one lock situation the thread can wake up if the wrong record is released and it has to wait again. Then some cpu cycles are consumed, but is that violating the requirement? 2) I have the user interface requirement "It must allow the user to book a selected record, updating the database file accordingly". Do you think this is ok solution: 1) The user selects the row 2) The user fills the owner field with the owner number 3) The user presses the book button 4) The book method is: -Locks the record -Reads the record -If the record is already booked throws the AlreadyBooked exception -Updates the record -Unlocks the record 3) How about the general user interface? How good it has to be? I don't have the tooltips, menu's, centering of the windows etc. Thanks, Mika

SC JP, SC JD, SC EA http://fi.linkedin.com/in/mikatapanainen Johnny Barbosa Greenhorn Joined: Sep 29, 2009 Posts: 26

posted 10/2/2009 12:40:27 AM

Do you think this is ok solution: 1) The user selects the row 2) The user fills the owner field with the owner number 3) The user presses the book button 4) The book method is: -Locks the record -Reads the record -If the record is already booked throws the AlreadyBooked exception -Updates the record -Unlocks the record 3) How about the general user interface? How good it has to be? I don't have the tooltips, menu's, centering of the windows etc.

www.coderanch.com/t/464865/java-developer-SCJD/certification/cpu-cycles-book-selected-record

2/6

6/13/13

1) no cpu cycles 2) book a selected record 3) the general user interface level (SCJD forum at JavaRanch)

Hi Mika, Look, do you agree with me, that, once time the record was booked, your application can to know if a record selected can be booked or not? So, not is necessary throws a "AlreadyBooked exception", because you can to prevent the user to book a record already booked, understand me? If the user select a record booked, at most that he can to do is to release that record, right?

C heers, Johnny Barbosa SC JA, SC JP, SC WC D, SC JD(Story | Relato), SC BC D (coming soon)

Roberto Perillo Bartender Joined: Dec 28, 2007 Posts: 2215


I like...

posted 10/2/2009 12:49:37 AM

Howdy, y'all.

Johnny Barbosa wrote:

Look, do you agree with me, that, once time the record was booked, your application can to know if a record selected can be booked or not?

Well, that could be done by calling the read() method, passing the number of the selected record. If this record is already booked (or the customer ID field is not empty), then it makes sense to throw an exception to the upper layer. The approach addressed by Mika looks pretty much like the one I implemented in my bookRoom() method of the business layer. Mika, just make sure to arrange your algorithm in a try/finally structure, so that the unlock() method is always called.

C heers, Bob "John Lennon" Perillo SC JP, SC WC D, SC JD, SC BC D - Daileon: A Tool for Enabling Domain Annotations Johnny Barbosa Greenhorn Joined: Sep 29, 2009 Posts: 26

posted 10/2/2009 1:19:01 AM

Well, that could be done by calling the read() method, passing the number of the selected record. If this record is already booked (or the customer ID field is not empty), then it makes sense to throw an exception to the upper layer. The approach addressed by Mika looks pretty much like the one I implemented in my bookRoom() method of the business layer.

Ok! In my Sun Interface not have a method like "reserve", but only "update".

Roberto Perillo Bartender

posted 10/2/2009 2:03:58 AM


3/6

www.coderanch.com/t/464865/java-developer-SCJD/certification/cpu-cycles-book-selected-record

6/13/13

1) no cpu cycles 2) book a selected record 3) the general user interface level (SCJD forum at JavaRanch)

Joined: Dec 28, 2007 Posts: 2215


I like...

Johnny Barbosa wrote:

Ok! In my Sun Interface not have a method like "reserve", but only "update".

Right... well, "reserve" looks more like a service that has done by the application, so it would be nice to define a layer that offers these services (such as booking or searching). There, we could implement all the required business logic to correctly perform these services.
Mika Tapanainen Ranch Hand Joined: Jun 11, 2009 Posts: 91

posted 10/2/2009 11:03:22 AM

Hello, my solution of the book method is like Roberto describes in his first reply. I'm also using try and finally. How about my other questions? Do you have opinions about them? Thanks, Mika

Roel De Nijs Bartender Joined: Jul 19, 2004 Posts: 4385


I like...

posted 10/2/2009 3:51:40 PM

Hi Mika,

Do you think so that 2 phase locking is really needed? I think my solution would be simpler if I would have only one lock. I know that in one lock situation the thread can wake up if the wrong record is released and it has to wait again. Then some cpu cycles are consumed, but is that violating the requirement?

I used synchronized-keyword and wait/notifyAll to make my Data class threadsafe. And I passed, so I think this approach is certainly not violating the requirements.

How about the general user interface? How good it has to be? I don't have the tooltips, menu's, centering of the windows etc.

It depends on how good you want to score of course. My Gui had everything: tooltips, mnemonics, menubar (with just 1 item), centering, dynamic buttons disabling/enabling, not allowing alphanumeric characters in numeric field (for example port number),...

www.coderanch.com/t/464865/java-developer-SCJD/certification/cpu-cycles-book-selected-record

4/6

6/13/13

1) no cpu cycles 2) book a selected record 3) the general user interface level (SCJD forum at JavaRanch)

Kind regards, Roel

SC JA, SC JP (1.4 | 5.0 | 6.0), SC JD http://www.javaroe.be/ Mika Tapanainen Ranch Hand Joined: Jun 11, 2009 Posts: 91

posted 10/2/2009 4:46:38 PM

Hi Roel, If I understood you correctly, you had the synchronization only on the database level, not on the record level? BR, Mika

Roberto Perillo Bartender Joined: Dec 28, 2007 Posts: 2215


I like...

posted 10/2/2009 6:03:14 PM

I used database level locking...

Roel De Nijs Bartender Joined: Jul 19, 2004 Posts: 4385


I like...

posted 10/2/2009 6:27:18 PM


Mika Tapanainen wrote:

If I understood you correctly, you had the synchronization only on the database level, not on the record level?

Indeed, my Data class was a singleton and all my methods were synchronized. The most easy approach as far as I know, maybe not the most performant and fast (but that wasn't a requirement, and simple, easy to understand code was). Kind regards, Roel

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

www.coderanch.com/t/464865/java-developer-SCJD/certification/cpu-cycles-book-selected-record

5/6

6/13/13

1) no cpu cycles 2) book a selected record 3) the general user interface level (SCJD forum at JavaRanch)

subject: 1) no cpu cycles 2) book a selected record 3) the general user interface level

Similar Threads Hand-over-hand locking not needed in SCJD book example ? Locking dumb question Will this locking mechanism be allowed? Removing entry from HashMap when storing ReentrantLocks safely B&S locking solution - consumes no CPU cycles
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jun 13, 2013 04:15:28 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/464865/java-developer-SCJD/certification/cpu-cycles-book-selected-record

6/6