Tuesday, February 12, 2008

Difference between Waiting and Yielding in JAVA Threads - Part 1


Waiting in Thread with Real world Metaphor:

Running Threads in JVM can be consider children playing in a park. As they are playing so they must be looking for some toys or rides in park. As we all know that there are some rides which can be shared by several children and some rides which can be used by only one child at a time. Lets call them shared~Rides and Rides respectively.

In this analogy -
> Park acting as JVM
> Children acting as Threads
> Shared~Rides acting as System Resources which can be shared by server children(Threads) at a time.
> Rides acting as System resources which can be used by one Children at a time.



Lets assume following scenario-

Two children A and B are playing. At this point of time A is playing with Ride_1 and B is playing with Ride_2. Suddenly B wants to play with Ride_1 so B looks at Ride_1 and what he sees? A is already playing with it, so B decides to wait until A finish with Ride_1. While B is waiting for A, he/she will opt either of the following activity:
1) Continue to use Ride_2 and when A finishes, Jump to Ride_1 and free the Ride_2 for other children.
2) Release Ride_2 for other children and patiently wait for A to finishes.

The Activity of B is referred as "WAITING".

Lets assume following scenario-

Imagine a group of children trying to buy ice cream from an ice cream truck. If the boy who currently has the ice cream vendor’s attention does not have enough money to pay for his ice cream, he won’t force everyone else to wait while his father brings him the funds he needs. He will get out the way, probably weep quietly to himself, and wait. Then he’ll step back into the fray and try to claim the ice cream vendor’s attention. The other children won’t wait for him, because he can’t order anyway.


In Technical world, if 2 threads are running and one thread wants to use a resource currently being used by another thread, and decides to wait until another threads finishes using the resource, in addition to this while waiting the threads may decides to either release or not to release the resource.

The Occupation of resource is referred as "LOCKING" in JAVA.

Waiting is the act of getting out of the way when you can’t be productive. As the name implies, waiting is an inactive process. This means that a waiting thread does not waste CPU time it cannot use. If a thread is waiting, then other threads are free to use its allotted quota of CPU time. The waiting thread will certainly not need it.

A Technical Example  :

 In this case, there is one thread representing the minister waiting for the collection to exceed $100. There are a further six threads representing attendees adding $20 to the collection plate. Each of these threads synchronizes on the collectionPlate object. Since the threads adding to the collection plate need to obtain the lock on the collectionPlate object, the minister thread must temporarily release it from time to time, which it does by calling the wait method. As each thread adds to the collection plate, it wakes the minister by calling the notify method.

Please see the attached image for source code



Calling the wait method is different than calling sleep or yield, or even pausing for I/O.
The wait method actually allows other threads to acquire the lock on the object in question,
while sleep, yield, and pausing for I/O do not.
============================================================== 
Click here : Yielding in JAVA
============================================================== 

5 comments:

Kshitija said...

You definitely are a good teacher... The examples are too good to understand and could be related to..
Thanks!!

Nuwan Armabage said...

it's really nice article , you digest the complexity and give it as a simple as possible.Nice work

Sunny said...

Thank you.

Javin Paul said...

Good post man keep it up.

Thanks
Javin Paul

Why String is immutable in Java

Jovial Java Admin said...

Thank you :)