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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the purpose of the file class in java programming?

767


How do I write a self declaration?

775


How do I know if java is installed?

791


What are the two ways of implementing multi-threading in java?

1174


What is string pooling concept?

792


What is a local class in java?

815


Differentiate between stringbuffer and string?

814


How do you sort arraylist in descending order?

800


How do you sing an Applet ?

2252


What about interrupt() method of thread class ?

856


What is prime number in java?

770


What is nan inf?

703


What does the append?

729


Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?

836


What does java final mean?

739