How two different class threads communicate with each
other?. send example code.

Answers were Sorted based on User's Feedback



How two different class threads communicate with each other?. send example code...

Answer / puneet khanna

The threads in java follow Mutual exclusion( synchronized)
and co-operation;

for co-operation they need to talk to each other by messages
saying " i am waiting in the wait q anyone can acquire the
lock of the object" or" i have acquired the lock now by
notify() to the last thread who called wait() or by
notifyall() to all the threads who are in the wait q"

Is This Answer Correct ?    4 Yes 0 No

How two different class threads communicate with each other?. send example code...

Answer / siva kumar g

By Using the concept called Inter Thread Communication

Is This Answer Correct ?    6 Yes 3 No

How two different class threads communicate with each other?. send example code...

Answer / modi[achir communication]

using wait() and notify() two different class threads
communicate with each other.
For example:

public class LoggingThread extends Thread {
private LinkedList linesToLog = new LinkedList();
private volatile boolean terminateRequested;

public void run() {
try {
while (!terminateRequested) {
String line;
synchronized (linesToLog) {
while (linesToLog.isEmpty())
linesToLog.wait();
line = (String) linesToLog.removeFirst();
}
doLogLine(line);
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}

private void doLogLine(String line) {
// ... write to wherever
}

public void log(String line) {
synchronized (linesToLog) {
linesToLog.add(line);
linesToLog.notify();
}
}
}

Is This Answer Correct ?    1 Yes 0 No

How two different class threads communicate with each other?. send example code...

Answer / tulasi prasad

Below I am showing the best example for ur Query.


class Rack
{
int n;
boolean avialable = false;
synchronized int get()
{
if(!avialable )
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("got:" + n);
avialable = false;
notify();
return n;
}

synchronized int put(int n)
{
if(avialable )
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
this.n = n;
avialable = true;
System.out.println("put:" + n);
notify();
return n;
}
}

class Producer implements Runnable
{
Rack k;
Producer(Rack k)
{
this.k = k;
new Thread(this, "Producer").start();
}
public void run()
{
int i = 1;
while(true)
{
k.put(i++);
}
}
}
class Consumer implements Runnable
{
Rack k;
Consumer(Rack k)
{
this.k = k;
new Thread(this, "Consumer").start();
}
public void run()
{
while(true)
{
k.get();
}
}
}
class ProducerConsumerProblem
{
public static void main(String args[])
{
Rack k = new Rack();
new Producer(k);
new Consumer(k);
System.out.println("press control - c to stop. ");
}
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

What are advantages and disadvantages of OOPs?

0 Answers   Amdocs,


Can inner class extend any class?

0 Answers  


What is jvm? Why is java called the platform independent programming language?

0 Answers  


What is the difference between static method and instance method in Java?

0 Answers   SwanSoft Technologies,


whats the life cycle of jsp

2 Answers   Satyam,


What does system.gc() and runtime.gc() methods do?

0 Answers  


How are destructors defined in java?

0 Answers  


In Inheritance if we are implementing Multi level inheritance and all class having same name of variable and now i want to access each class variable and how it is possible?

2 Answers  


What is meant by overloading?

0 Answers  


If A Class Is Declared Without Any Access Modifiers, Where May The Class Be Accessed?

0 Answers   Wipro,


What is difference between overloading and overriding?

2 Answers   Accenture,


What is difference between fail-fast and fail-safe?

0 Answers  


Categories