jQuery in Action, 3rd edition
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes URLyBird 1.1.2 - Locking Clarification... 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 "URLyBird 1.1.2 - Locking Clarification..." Watch "URLyBird 1.1.2 - Locking Clarification..." New topic

URLyBird 1.1.2 - Locking Clarification...

Jim Taylor

Joined: Feb 02, 2007
Posts: 1
My assignment includes the following statement in its Locking requirement...

"Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available."

At first I took this to mean that I'd have to implement "wait()/notify()" everywhere that I "synchronized()" on the static Map containing my locked record info. However, I'm starting to question my assumption and think that "wait()/notify()" might not be necessary...

I have recently played around with deadlocking and have noticed that when I intentionally cause a deadlock to occur, the CPU cycles drop to 0%. It seems that both of my threads are waiting on the other to release there lock and while they are waiting they consume no CPU cycles.

Therefore, to keep things as simple as possible, I'm wondering whether I could avoid "wait()/notify()" entirely (at least in terms of locking) and just rely on "synchronized()" blocks of code to satisfy the requirement.

I've read MANY chapters on Threads & Locking, but still feel that my knowledge of when to apply "wait()/notify()/notifyAll()" is still lacking.

Any guidance is appreciated. Thanks in advance.
Alex Duran

Joined: Jan 29, 2007
Posts: 8
There are many posts in the forum concerning the locking design. I think this one is particularly useful: http://www.coderanch.com/t/187926/java-developer-SCJD/certification/Locking-Approach

It help me to think of the wait/notifyAll mechanism as a means by which threads communicate with one another.

Basically, as I see it, threads often encounter similar conditions where its work cannot proceed without a change in the state of some object or other. The notifyAll method is sort of like a thread making a general announcement to all threads that it's changed the state of an object such that the condition on which one or more threads has been waiting - may now be satisfied.

Looking at the "Threads" Chapter in "The Java Programming Language, Third Edition", the authors characterize the pattern as:

[ February 02, 2007: Message edited by: Barry Gaunt ]

Alex Duran

Joined: Jan 29, 2007
Posts: 8
I missed a '{' after 'doWhenCondition()' above.

(No - you didn't -- use the paper/pencil icon to edit your posts.)
[ February 02, 2007: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you have general problems on threads, can I suggest that you make use of our Threads forum where Henry Wong will be pleased to answer your questions.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link: http://aspose.com/file-tools
subject: URLyBird 1.1.2 - Locking Clarification...
It's not a secret anymore!