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 are anonymous inner classes?
What is difference between static and abstract class?
What is an abstract class and what is it’s purpose?
What is a function argument in java?
What are kinds of processors?
What environment variables do I need to set on my machine in order to be able to run java programs?
What is the importance of finally block in exception handling?
What do you understand by the term singleton?
Can we inherit inner class?
Tell us something about different types of casting?
What is the difference between hashset and treeset in java?
Is ++ operator is thread safe in java?
Explain notify() method of object class ?
Describe different states of a thread.
Explain all java features with real time examples