Hi I was wondering if we could discuss a program and try to understand why the author may have chosen to write the program in that way.
The code and an explanation is at:
http://java.oreilly.com/bite-size/java_0299.html A couple of things first, I'm wondering why it was written like this:
while (true) addClient(serverSocket.accept());
as opposed to:
while (true) {
Socket connection = serverSocket.accept();
addClient(connection);
}
another thing is, I couldn't quite understand why this was written the way it was, is it normal or is there a neater way?
public abstract class Listener {
public abstract void processLine(
String line);
private BufferedReader mIn;
public Listener(InputStream in) {
mIn = new BufferedReader(new InputStreamReader(in));
Thread t = new Thread() {
public void run() {
try {
String line;
while ((line = mIn.readLine()) != null) processLine(line);
}
catch (IOException ioe) {}
}
};
t.start();
}
}
When an instance of Listener is made in the addClient() it is anonymous why?
new Listener(clientSocket.getInputStream()) {
public void processLine(String line) {
if (line.length() == 0) mClients.remove(out);
else sendToClients(line);
}
};
Thanks for your time.