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 the purpose of the file class in java programming?
How do I write a self declaration?
How do I know if java is installed?
What are the two ways of implementing multi-threading in java?
What is string pooling concept?
What is a local class in java?
Differentiate between stringbuffer and string?
How do you sort arraylist in descending order?
How do you sing an Applet ?
What about interrupt() method of thread class ?
What is prime number in java?
What is nan inf?
What does the append?
Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?
What does java final mean?