• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Newbie questions on Threads and Synchronization

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the classic producer comsumer examples used in many books when explaining Synchronization, the Synchronized methods always begin with a while loop that checks a condition and then calls the wait() method.
When that thread gets notified by another thread, where does the waiting thread resume execution from? Does it beging at the beginning of the synchronised method or at the line just after the wait() method ?
What is the reason for using a while loop and not an if statement to check if it is OK to produce or consume?
If there are multiple threads waiting and all of them get notified, do they all become "Ready" and then wait for the scheduler to allow one of them to run? At what point does a thread that became ready to run, starts to process the Synchronized method?
What happens to all the other threads that became
"Ready" when they were notified?
Thanks
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should think of the Thread that calls wait() as "stuck" in the wait method. When it gets to run it returns from the wait() call.
A while loop is typical for a process that has to repeat.
The multiple threads question is why there is a notify() and a notifyAll() method. If all Threads are notified, they all become ready to run and blocked trying to return from wait to enter the synchronized code again. It avoids some overhead to call notify() and just activate one Thread.
Bill
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have found all your questions very interesting and sensible.
There are a set of articles called "Programming with threads in the real world (1-8)" in javaworld.com that I recommend to you.
I will try to solve your doubts:

What is the reason for using a while loop and not an if statement to check if it is OK to produce or consume?

This is explained in the articles. Suppose there are several threads waiting for the same lock. And notifyAll is called on that lock. All the threads leave the wait state and enter the ready one. Now the schedules chooses one thread to execute. The others are still in the ready state and they could run when the lock becomes available again (the winner thread ends its run method, for instance). Because the call to notifyAll really represents that the condition that halted the progress of the waiting threads has been satisfied, one thread executes. But the notification of the end of the wait is only for one thread, all the others should check again that the condition to progress is satisfied. And this is why the wait call is within a loop that checks the condition to progress.

If there are multiple threads waiting and all of them get notified, do they all become "Ready" and then wait for the scheduler to allow one of them to run? At what point does a thread that became ready to run, starts to process the Synchronized method?

It enters the competition to be chosen by the scheduler with no priveleges to respect the others already in the ready state.

What happens to all the other threads that became
"Ready" when they were notified?

If the loop is used, they go to wait because the condition is checked again.
Otherwise they could be executing the method when they shouldn't because the satisfaction of the condition is for only one thread. This is imposed by the "protocol" of the comsumer-producer scenario.
[ January 18, 2002: Message edited by: Jose Botella ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic