Java Reference
In-Depth Information
* will be initialized with the name of a host and a team, and will
* be started so that it can get the roster for that team from the
* {@link StatReporter} running on that host (if there is one). This
* method will start a separate thread for the retrieval, and will
* return while that thread is running.
* @param fromHost name of the host to contact looking for a
* {@link StatRecorder} to obtain the roster
* @param team Name of the team whose roster is desired
* @return the {@link RosterRetriever} created
*/
private RosterRetriever startRosterLookup(String fromHost, String team) {
RosterRetriever getter = new RosterRetriever(fromHost, team);
getter.start();
return getter;
}
This method will create a
RosterRetriever
, initializing the team and host names, and then
start a thread that will perform the
run()
method of that class. It will return the
Roster-
Retriever
object that was created, which we will stash someplace (a
Hashtable
mapping
team names to
RosterRetriever
objects associated with that team would be a good candid-
ate), and then our code could go off and do other things while this new thread makes the re-
mote calls to get the roster for the team.
When we actually want to find the roster for a team, we will use the
RosterRetriever
object
that was started up with that team's name. Prior to trying to get the roster, we will call the
isDone()
method, which will tell us whether the task has completed. If that comes back
true
,
we can then call the
getRoster()
method. If that returns a value of
null
, we know that
something went wrong. If we really wanted to know, we could add an
Exception
field to
our
RosterRetriever
and a method to return that
Exception
object. More likely we will get
back the
List
of
Player
objects that is the roster for the team. Best of all, we didn't have to
wait while we got the roster. Our program could continue doing something else while all that
was going on in a separate thread.
We can also try to get an updated roster by creating a new
RosterRetriever
object for a
particular team (and host) and, when we think an update might be needed, calling
start()
on that object. We can create a new
RosterRetriever
object and
start()
it any number
of times; each time we do, a new thread object will be created, started, and the remote calls
made.