RuneHive-Game
Loading...
Searching...
No Matches
StartupService.java
Go to the documentation of this file.
1package com.runehive.game.service;
2
3import java.util.Collection;
4import java.util.List;
5import java.util.concurrent.*;
6
7/**
8 * An <code>ExecutorService</code> that waits for all its events to finish
9 * executing.
10 *
11 * @author Graham Edgecombe
12 */
13public class StartupService implements ExecutorService {
14
15 /** The service backing this service. */
16 private ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
17
18 /** A list of pending tasks. */
19 private BlockingQueue<Future<?>> pendingTasks = new LinkedBlockingQueue<>();
20
21 /**
22 * Waits for pending tasks to complete.
23 *
24 * @throws ExecutionException
25 * if an error in a task occurred.
26 */
27 public void waitForPendingTasks() throws ExecutionException {
28 while (pendingTasks.size() > 0) {
29 if (isShutdown()) {
30 return;
31 }
32 try {
33 pendingTasks.take().get();
34 } catch (InterruptedException e) {
35 continue;
36 }
37 }
38 }
39
40 public void awaitUntilFinished(long timeout, TimeUnit timeUnit) {
41 try {
42 service.awaitTermination(timeout, timeUnit);
43 } catch (InterruptedException e) {
44 e.printStackTrace();
45 }
46 }
47
48 /**
49 * Gets the number of pending tasks.
50 *
51 * @return The number of pending tasks.
52 */
53 public int getPendingTaskAmount() {
54 return pendingTasks.size();
55 }
56
57 @Override
58 public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
59 return service.awaitTermination(timeout, unit);
60 }
61
62 @Override
63 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {
64 List<Future<T>> futures = service.invokeAll(tasks);
65 for (Future<?> future : futures) {
66 pendingTasks.add(future);
67 }
68 return futures;
69 }
70
71 @Override
72 public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
73 throws InterruptedException {
74 List<Future<T>> futures = service.invokeAll(tasks, timeout, unit);
75 for (Future<?> future : futures) {
76 pendingTasks.add(future);
77 }
78 return futures;
79 }
80
81 @Override
82 public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {
83 return service.invokeAny(tasks);
84 }
85
86 @Override
87 public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
88 throws InterruptedException, ExecutionException, TimeoutException {
89 return service.invokeAny(tasks, timeout, unit);
90 }
91
92 @Override
93 public boolean isShutdown() {
94 return service.isShutdown();
95 }
96
97 @Override
98 public boolean isTerminated() {
99 return service.isTerminated();
100 }
101
102 @Override
103 public void shutdown() {
104 service.shutdown();
105 }
106
107 @Override
108 public List<Runnable> shutdownNow() {
109 return service.shutdownNow();
110 }
111
112 @Override
113 public <T> Future<T> submit(Callable<T> task) {
114 Future<T> future = service.submit(task);
115 pendingTasks.add(future);
116 return future;
117 }
118
119 @Override
120 public Future<?> submit(Runnable task) {
121 Future<?> future = service.submit(task);
122 pendingTasks.add(future);
123 return future;
124 }
125
126 @Override
127 public <T> Future<T> submit(Runnable task, T result) {
128 Future<T> future = service.submit(task, result);
129 pendingTasks.add(future);
130 return future;
131 }
132
133 @Override
134 public void execute(Runnable command) {
135 service.execute(command);
136 }
137
138}
An ExecutorService that waits for all its events to finish executing.
BlockingQueue< Future<?> > pendingTasks
A list of pending tasks.
public< T > Future< T > submit(Runnable task, T result)
void waitForPendingTasks()
Waits for pending tasks to complete.
ExecutorService service
The service backing this service.
boolean awaitTermination(long timeout, TimeUnit unit)
public< T > Future< T > submit(Callable< T > task)
int getPendingTaskAmount()
Gets the number of pending tasks.
public< T > List< Future< T > > invokeAll(Collection<? extends Callable< T > > tasks, long timeout, TimeUnit unit)
public< T > T invokeAny(Collection<? extends Callable< T > > tasks, long timeout, TimeUnit unit)
public< T > List< Future< T > > invokeAll(Collection<? extends Callable< T > > tasks)
public< T > T invokeAny(Collection<? extends Callable< T > > tasks)
void awaitUntilFinished(long timeout, TimeUnit timeUnit)