Yielding
Yielding is the act of politely offering up your turn in the queue while maintaining your resources.
For example, suppose there are several person queuing up to use a bank’s automated teller machine(ATM).
In the Analogy-
People in Queue acting as Running Threads.
ATM acting as CPU.
Bingo! Your turn has arrived to use ATM. You know that this process will take a while because you’re going to transfer funds,check your balance, make a deposit, and so forth. So you decides to offer the person behind you to use ATM before you. This activity is referred as "YIELDING".Of course, you wouldn’t give that person your resources (money, ATM card, and so on). However, you would offer to give up your spot at the ATM (analogous to the CPU). The process of making this offer,even if there’s no one to accept it, is yielding. It may possible that person behind you is too nice and he/she says that please continue to use ATM, I would rather wait for you to finish. So it is possible that, even though you yielded, you may still get first access. In computer terms, though, when you yield, all threads (including your own) then get to compete for the lock on the object—your thread does not get a lesser or greater chance of getting the lock on the object. But what decides which thread actually wins that competition and gets access to the lock on the object? The thread scheduler does.
The thread scheduler is like an office manager. It takes threads into consideration and decides which thread should be allowed to run based on the underlying operating system, thread priorities, and other factors. There are two important points to note-
First, you must be aware that your thread of execution is not guaranteed to be next in line when you yield, even though you originally volunteered to give up the CPU. Once a thread yields and the CPU is taken by another thread, the original yielding thread does not have any special status in being next in line. It must wait and hope, like every other thread.
Second, it’s important to realize that your thread didn’t give up resources that you have exclusive locks on. That is, even though you’re letting someone else use the ATM, you didn’t give him your ATM card. When a thread yields, it steps out of the CPU, but it retains control over any resources it had originally marked for exclusive use.
Showing posts with label waiting. Show all posts
Showing posts with label waiting. Show all posts
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.
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
==============================================================
Subscribe to:
Posts (Atom)