promise: Use executor when scheduled executor rejects
Rather than run the operation on the current thread, if the scheduled
executor rejects the operation, we then attempt the run the operation
on the executor.
@@ -128,18 +128,27 @@ abstract class PromiseImpl<T> implements Promise<T> {
* the queue and executing them, so the order in which callbacks are
* executed cannot be specified.
*/
- for (Runnable callback = callbacks.poll(); callback != null; callback = callbacks.poll()) {
+ for (Runnable callback; (callback = callbacks.poll()) != null;) {
+ execute(callback);
+ }
+ }
+
+ /**
+ * Execute a operation on the executor.
+ *
+ * @since 1.2
+ */
+ void execute(Runnable operation) {
try {
try {
- factory.executor().execute(callback);
+ factory.executor().execute(operation);
} catch (RejectedExecutionException e) {
- callback.run();
+ operation.run();
}
} catch (Throwable t) {
uncaughtException(t);
}
}
- }
/**
* Schedule a operation on the scheduled executor.
@@ -153,7 +162,7 @@ abstract class PromiseImpl<T> implements Promise<T> {
return factory.scheduledExecutor().schedule(operation, delay,
unit);
} catch (RejectedExecutionException e) {
- operation.run();
+ execute(operation);
}
} catch (Throwable t) {
uncaughtException(t);
