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
what is singleton in java?
What do you understand by the term singleton?
Explain about fail safe iterators in java?
Explain about complier design(phases)
What are disadvantages of java?
What is difference between call by value and call by reference?
Why volatile is used in java?
What is string intern in java?
What is an i/o filter?
What are the ways to instantiate the class class?
Compare overloading and overriding?
What is a Null object?
What is multiple inheritance? Is it supported by java?
How do you calculate square roots?
why Java does not support multiple inheritances?