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
Write a code to show a static variable?
What is a finally block? Is there a case when finally will not execute?
Can we convert list to set in java?
What do you understand by private, protected and public?
How do you sort a string in alphabetical order in java?
What are different types of arrays?
How do you convert boolean to boolean?
What is string value?
How can you write a loop indefinitely in java programming?
what type of questions asked for barclays technologies pune please send urgent
Describe what happens when an object is created in java ?
What is e in java?
Is java type safe?
What is connection class in java?
Explain the pointers in Java?