Global Positioning System Reference
In-Depth Information
protected Executor serverQueue
= Executors.newSingleThreadExecutor();
The thread pool \creates an Executor that uses a single worker thread oper-
ating o an unbounded queue" [see javadoc, Executors>newSingleThread
Executor ] or a thread pool with a fixed number of threads:
protected Executor serverQueue
= Executors.newFixedThreadPool( nrOfParallelThreads );
The identification of the player client could be added with
Runnable getClientID = new Runnable() {
public void run() { candidate.getIdentity(); }};
serverQueue.execute( getClientID );
This construction is a bit simpler than DeferredThread method used earlier.
A deferral could be placed inside the run() method,
...
public void run() {
try { Thread.sleep( 1000 ); } // defer call
catch (InterruptedException e) {}
candidate.getIdentity();
}
to slow down a single worker thread, while a thread pool can still accept
other requests.
Probably the best way to do this is to use a scheduled
thread pool
protected ExecutorService serverQueue =
Executors.newScheduledThreadPool( nrOfParallelThreads );
A scheduled thread pool \creates a thread pool that can schedule com-
mands to run after a given delay, or to execute periodically" [see javadoc,
Executors>newScheduledThreadPool ].
For the identification, the Runnable interface was used, which is usually
associated with Thread s. Actually, it is not tied to threads, so it's ne
to use it for executors. The Runnable.run() method imposes limits on the
implementation as it can not return a value and can not throw an exception.
These limits can be overcome by using the concept of the Executor
Service , Callable , and Future classes. Just like Runnable , the Callable
interface describes tasks to be execute d or submit ted. The callable in-
terface makes use of generics (which weren't available when Runnable was
introduced) to supply return value and exception with the implementation:
public interface Runnable
{ void run(); }
public interface Callable<V>
{ V call() throws Exception; }
 
Search WWH ::




Custom Search