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 a databasemetadata?
Is java based on c?
What is the java idl system?
What is default locale java?
Define jit compiler?
How many times finalize method will be invoked? Who invokes finalize() method in java?
Is it possible to write a regular expression to check if string is a number?
What are abstract methods in java?
Will the compiler creates a default constructor if I have a parameterized constructor in the class?
What is a java predicate?
What is the function of java?
What is ctrl m character?
Explain the key functions of data binding?
What is the longest unicode character?
What is the inheritance?