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
How many types of methods are there?
How many types of the indexof method are there for strings?
Can we have multiple classes in a single file?
Does java support function overloading, pointers, structures, unions or linked lists?
Can a variable be local and static at the same time?
What is the statements?
How we can make copy of a java object?
What is the advantage of preparedstatement over statement?
What is the differences between c++ and java? Explain
Is empty list java?
What is the use of flag?
What are the steps involved to create a bean?
What is a map? What are the implementations of map?
Define iterator and methods in iterator?
Explain the importance of finalize() method.