Java Reference
In-Depth Information
Becauseonlineaccesscanbeslow,andbecausetheuserinterfaceshouldremainre-
sponsive (perhaps the user might want toendthe application), youoffload the “obtain
wordentries”tasktoanexecutorthatrunsthistaskonaseparatethread.Thefollowing
exampleemploys ExecutorService , Callable ,and Future toaccomplishthis
objective:
ExecutorService executor = ...; // ... represents some ex-
ecutor creation
Future<String[]>
taskFuture
=
executor.submit(new
Callable<String[]>()
{
public
String[] call()
{
String[]
entries = ...;
//
Access online dictionaries
//
with search word and populate
//
entries with their resulting
//
entries.
re-
turn entries;
}
});
// Do stuff.
String entries = taskFuture.get();
After obtaining an executor in some manner (you will learn how shortly), the ex-
ample's main thread submits a callable task to the executor. The submit() method
immediatelyreturnswithareferencetoa Future objectforcontrollingtaskexecution
and accessing results. The main thread ultimately calls this object's get() method to
get these results.
Search WWH ::




Custom Search