Why are wait(), notify() and notifyAll() methods defined in the object class?

Ans: The purpose of the wait(), notify() and notifyAll() methods is to temporarily pause and resume the execution of code in an object. Typically the host object is not in a state where it can proceed with a method call it has been given and the thread of execution must literally wait for the object to return to a ready state. A common example would be a limited pool or store of objects where you must wait for a storage slot to be released or an object to be returned to the pool before you can use it.

public synchronized Object getNextObject() {

// Waiting loop
while (! objectAvailable()) {

try {

wait();
}
catch (InterruptedException e) {

// Handle exception
}
}

// No longer waiting, get the return object
Object returnObject;

// Assign the returnObject from store

// Notify state change for other waiters
notify();

return returnObject;
}
The act of waiting is associated with the Object class because any subclass may need to wait for a ready state to occur (Java is fundamentally a multi-threaded language). The waiting process acts on a single thread of execution, but the wait mechanism expects that multiple threads may be waiting for the same object. The wait() and notify() methods are hosted by the Object class so that the Java Virtual Machine can manage the “wait set” of threads through the objects they are waiting for.

People who read this post also read :



6 comments:

A Remarkable Relocation Guide Suitable For You

packers and movers pune
Welcome to here, a premium online tool helping consumers relocate from one place to another with household belongings or commercial commodities. We have made it easy to find right services, right moving companies and other related information in different major cities and towns of India packers and movers in pune.
movers and packers in pune

chung cư Eco Dream http://www.chungcu-ecodream.com/ cập nhật bảng giá mới nhất

Hi, great post! By the way, if perhaps you're looking for the best software developers in Poland, reach out to Pro4People - you'll find them right here. They did an amazing job for my business!

Strange "water hack" burns 2lbs overnight

More than 160 thousand women and men are using a easy and secret "liquids hack" to burn 1-2lbs each night as they sleep.

It's effective and it works on anybody.

You can do it yourself by following these easy steps:

1) Go grab a drinking glass and fill it up half full

2) Proceed to learn this awesome hack

so you'll be 1-2lbs skinnier the next day!

Did you hear there's a 12 word sentence you can speak to your man... that will trigger intense feelings of love and impulsive attraction to you buried inside his chest?

Because hidden in these 12 words is a "secret signal" that triggers a man's impulse to love, treasure and protect you with all his heart...

=====> 12 Words That Fuel A Man's Love Response

This impulse is so hardwired into a man's mind that it will drive him to try harder than ever before to take care of you.

As a matter of fact, triggering this powerful impulse is absolutely important to having the best ever relationship with your man that the second you send your man one of these "Secret Signals"...

...You will immediately notice him expose his mind and soul to you in such a way he never expressed before and he will identify you as the only woman in the world who has ever truly understood him.

As reported by Stanford Medical, It is indeed the ONLY reason this country's women live 10 years more and weigh an average of 42 lbs less than we do.

(And by the way, it has totally NOTHING to do with genetics or some secret-exercise and EVERYTHING related to "HOW" they eat.)

P.S, What I said is "HOW", not "what"...

CLICK this link to see if this quick questionnaire can help you decipher your true weight loss possibility

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More