Java Reference
In-Depth Information
a StatRecorder object to get the roster for a team, waiting until the call finished to do any-
thing else. This might be fine if both the objects are on a local network. But if there is a more
complex network between them, this call might take some time.
A different approach would be to build a class within the StatReporterImpl object that can
be used to make the remote calls in a separate thread while the StatReporterImpl object does
something else. We will make this class extend the base class Thread , and do all the remote
work that we had been doing in the StatReporterImpl object. The class looks something
like:
/**
* A private inner class that can be used to obtain the
* roster of a team. This class can be started on its
* own thread and left to do its work. When the results
* are needed, you can find out if the work is done by
* checking the results of {@link isDone}. Once the
* remote calls have completed, the value of the team
* roster can be obtained by calling {@link getRoster}. Failure
* will be indicated by a value of {@code true} for {@link isDone}
* and a value of {@code null} for {@code getRoster}.
*/
private class RosterRetriever extends Thread {
private String fromHost = null;
private String forTeam = null;
private Set<Player> roster = null;
private boolean done = false;
RosterRetriever(String fromHost, String team) {
this.fromHost = fromHost;
forTeam = team;
}
@Override
public void run(){
try {
Registry useRegistry = LocateRegistry.getRegistry(fromHost);
StatRecorder recorder =
(StatRecorder) useRegistry.lookup("Recorder");
roster = recorder.getRoster(forTeam);
done = true;
} catch (Exception e) {
System.out.println("Unable to get roster for team " + forTeam);
done = true;
Search WWH ::




Custom Search