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 are JTA/JTS and how they used by client?

1720


What is the difference between RMI registry and OS Agent?

634


How will you pass parameters in RMI? Why do you serialize?

700


when A client sent a request to the server to open facebook page and close the browser after this request .at that time the same user do login by using a different browser then that session id will exist or not for the same client??

1106


What is message driven beam?

585






Difference between swing and awt?

603


What are externizable interface?

586


What is scalable, portability in the view of J2EE?

1879


To identify IDL language what mapping mechanism is used?

3419


Why a client should be multithreading? Explain.

597


In inglish: How to convert jar to exe files? Em português: Como converter arquivos .jar para .exe?

2158


What is ioc concept?

693


What is colon_pkg_prefixes and what is its use?

1999


What does module-relative mean?

577


How a component can be placed on Windows?

2266