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 carriage return means?

909


Why method overriding is used?

1020


What do you mean by platform independence?

990


Explain runtime exceptions?

1109


Is java a utf 8 string?

930


What languages are pass by reference?

989


What is a data structure java?

952


What are class members by default?

1021


Can we declare a constructor as final?

1206


What is collection class in java?

930


Can a class extend more than one class?

1052


What does super keyword do?

1015


Will minecraft java be discontinued?

1114


Difference between final and effectively final ?

1024


How can we make string upper case or lower case?

1025