• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

URLyBird 1.1.2 - Locking Clarification...

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are many posts in the forum concerning the locking design. I think this one is particularly useful: https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Whatever. Here's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic