• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

multithreading

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

someone please explain me how these get() and put() methods will work, I am confused in their Boolean test ?
please explain me it with some 5-6 iterations
thanks
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you start 2 threads: Producer and Consumer and they call different method: put and get, this method synchronized on instance of Q.
you cant be sure what method will be called first.
but boolean value = false;
and test inside you can be sure that thread that call get will wait if it called first.

thread will wait for notify and getting lock, and that check condition again.

so when other thread get lock:

will check condition, but this time no wait will execute because initially while value = false.
you set n variable and change value to true and notify other thread if any in waiting state.
if reverse order of starting you will have same behavior.
Hope this help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic