Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What is a databasemetadata?

903


Is java based on c?

947


What is the java idl system?

997


What is default locale java?

1054


Define jit compiler?

1070


How many times finalize method will be invoked? Who invokes finalize() method in java?

972


Is it possible to write a regular expression to check if string is a number?

980


What are abstract methods in java?

1074


Will the compiler creates a default constructor if I have a parameterized constructor in the class?

1052


What is a java predicate?

922


What is the function of java?

955


What is ctrl m character?

1003


Explain the key functions of data binding?

971


What is the longest unicode character?

1105


What is the inheritance?

936