How two threads will communicate with each other?

Answer Posted / vivek ghavle

The Object class has three methods, wait(), notify(), and
notifyAll() that help threads
Communicate.
Suppose a mail processor thread has to keep checking if
there is any mail to process.
For example,
if one thread is a mail-delivery thread and one thread is a
mail-processor thread,
the mail-processor thread has to keep checking to see if
there's any mail to process.
Using the wait and notify mechanism, the mail-processor
thread could check for
mail, and if it doesn't find any it can say, "Hey, I'm not
going to waste my time
checking for mail every two seconds. I'm going to go hang
out, and when the mail
deliverer puts something in the mailbox, have him notify me
so I can go back to
runnable and do some work."
In other words, using wait() and notify() lets one thread
put itself into a "waiting room" until some other thread
notifies it that there's a reason to come back out.
wait(), notify(), and notifyAll() must be called from within
a synchronized
context! A thread can't invoke a wait or notify method on an
object unless it owns
that object's lock.

Is This Answer Correct ?    3 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between long.class and long.type?

553


What do you know about seam?

590


What is the highest-level event class of the event-delegation model?

564


Explain phantom read?

1731


A user of a web application sees a jsessionid argument in the URL whenever a resource is accessed. What does this mean? a. The form must have the field jsessionid b. URL rewriting is used as the session method c. Cookies are used for managing sessions

1807






What are transaction attributes?

608


Whats new with the stop(), suspend() and resume() methods in jdk 1.2?

574


What is the diffrence between a local-tx-datasource and a xa-datasource? Can you use transactions in both?

564


what is a portable component?

1458


What are the pros and cons of detached objects?

541


How database connectivity in XML is achieved?

1762


What is the purpose of the notifyall() method?

599


What is the relationship between an event-listener interface and an event-adapter class?

548


Is infobus easy to use?

592


What is glasgow?

576