Java Reference
In-Depth Information
}
}
boolean isDone(){
return done;
}
Set<Player> getRoster(){
return roster;
}
}
The idea behind the use of this class is that we will create a single object instance for each
team and
StatRecorder
object-containing host that we want to query. The
run()
method
of this object tries to contact the host that was supplied as part of the constructor to find a
StatRecorder
server. If one is found, the method will then attempt to get the roster for the
Team
whose name was given as part of the constructor. Once the method is finished, either
successfully or when an exception has been thrown, the
boolean done
is set to
true
and the
method (and the thread) finishes. Note that the
run()
method takes no parameters and returns
nothing; all of the work of setup is accomplished before the method is called, and all of the
results are stored as part of the object state. In this sense, a method like this is something like
the completions found in other languages. But the real reason to have such an object is to al-
low it to be run concurrently with other things that our program might be doing.
We can then call
start()
on a
RosterRetriever
object, which will execute the
run()
meth-
od. The
start()
method will return as soon as the thread is started, and we can run in paral-
lel with that thread in our main program. When we actually need the roster, we will first see
whether the work is complete by calling
isDone()
. If the result we get back is
true
, we can
then call
getRoster()
. If the result of this call is
null
, we know that something went wrong.
But if we get a
Set
of
Player
objects back, we will have gotten the roster for the team in
parallel with doing other things.
We can make
RosterRetriever
an inner class that is seen only in the scope of the
StatRe-
porterImpl
object. This will ensure that only individual objects of the
StatReporterImpl
class will use this kind of object. To use a
RosterRetriever
, we could add a method to the
StatReporterImpl
that looks something like:
/**
* Create and start a {@link RosterRetriever}. The created object