Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLyBird 1.1.2 - Locking Clarification...

 
Jim Taylor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic