How two threads will communicate with each other?

Answers were Sorted based on User's Feedback



How two threads will communicate with each other?..

Answer / bindhu

Two threads can communicate with each other using
the wait() and notify() , notifyAl() methods. wait()
method tells the thread to wait until another threads
calls notify() method.

The wait() method causes the current thread to wait
(possibly forever) until another thread notifies it of a
condition change. We use wait() in conjunction with notify
() or notifyAll() to coordinate the activities of multiple
threads using the same resources.

The notifyAll() method notifies all the threads waiting on
the monitor held by the current thread and wakes them up.
Typically, one of the waiting threads will grab the monitor
and proceed.

Is This Answer Correct ?    57 Yes 8 No

How two threads will communicate with each other?..

Answer / 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

More Advanced Java Interview Questions

int x=5,i=1,y=0; while(i<=5) { y=x++ + ++x; i=i+2; } System.out.println(x); System.out.println(y); System.out.println(i); How to solve this? Please explain!

0 Answers  


Explain the different types of memory used by jvm?

0 Answers  


what do u mean by java bean??

2 Answers   ADP,


Thread life cycle?

3 Answers   TCS,


What you mean by COM and DCOM?

0 Answers  






What is the immediate superclass of the applet class?

0 Answers  


How two threads will communicate with each other?

2 Answers  


What will happens, when a thread cannot acquire a lock on an object?

2 Answers  


Different between Struts and Spring? or Why use Spring, if you are already using Struts?

3 Answers   HP,


If we opened Windows Internet Explorer 4 times, does it starts 4 processes or 4 threads?

0 Answers  


What is meant by multi-threading?

6 Answers  


what is the use of State Factories?

0 Answers  


Categories