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 carriage return means?
Why method overriding is used?
What do you mean by platform independence?
Explain runtime exceptions?
Is java a utf 8 string?
What languages are pass by reference?
What is a data structure java?
What are class members by default?
Can we declare a constructor as final?
What is collection class in java?
Can a class extend more than one class?
What does super keyword do?
Will minecraft java be discontinued?
Difference between final and effectively final ?
How can we make string upper case or lower case?