@@ -17,7 +17,7 @@ import scala.language.implicitConversions
1717import scala .concurrent .java8 .FuturesConvertersImpl ._
1818import scala .concurrent .java8 .FuturesConvertersImplCompat ._
1919import scala .concurrent .{ Future , Promise , ExecutionContext , ExecutionContextExecutorService , ExecutionContextExecutor }
20- import java .util .concurrent .{ CompletionStage , Executor , ExecutorService }
20+ import java .util .concurrent .{ CompletionStage , Executor , ExecutorService , ForkJoinPool }
2121import java .util .function .Consumer
2222
2323/**
@@ -59,16 +59,38 @@ object FutureConverters {
5959 * transformations to their asynchronous counterparts, i.e.
6060 * <code>thenRun</code> will internally call <code>thenRunAsync</code>.
6161 *
62+ * Callbacks will run onto ForkJoinPool.commonPool(), unless it does not
63+ * support a parallelism level of at least two, in which case a new Thread
64+ * is used.
65+ *
6266 * @param f The Scala Future which may eventually supply the completion for
6367 * the returned CompletionStage
6468 * @return a CompletionStage that runs all callbacks asynchronously and does
6569 * not support the CompletableFuture interface
6670 */
67- def toJava [T ](f : Future [T ]): CompletionStage [T ] = {
71+ def toJava [T ](f : Future [T ]): CompletionStage [T ] = toJava(f, ForkJoinPool .commonPool())
72+
73+ /**
74+ * Returns a CompletionStage that will be completed with the same value or
75+ * exception as the given Scala Future when that completes. Since the Future is a read-only
76+ * representation, this CompletionStage does not support the
77+ * <code>toCompletableFuture</code> method. The semantics of Scala Future
78+ * demand that all callbacks are invoked asynchronously by default, therefore
79+ * the returned CompletionStage routes all calls to synchronous
80+ * transformations to their asynchronous counterparts, i.e.
81+ * <code>thenRun</code> will internally call <code>thenRunAsync</code>.
82+ *
83+ * @param f The Scala Future which may eventually supply the completion for
84+ * the returned CompletionStage
85+ * @param e The Java Executor onto which run the callbacks
86+ * @return a CompletionStage that runs all callbacks asynchronously and does
87+ * not support the CompletableFuture interface
88+ */
89+ def toJava [T ](f : Future [T ], e : Executor ): CompletionStage [T ] = {
6890 f match {
6991 case p : P [T @ unchecked] => p.wrapped
7092 case _ =>
71- val cf = new CF [T ](f)
93+ val cf = new CF [T ](f, e )
7294 implicit val ec = InternalCallbackExecutor
7395 f onComplete cf
7496 cf
@@ -189,10 +211,30 @@ object FutureConverters {
189211 * transformations to their asynchronous counterparts, i.e.
190212 * <code>thenRun</code> will internally call <code>thenRunAsync</code>.
191213 *
214+ * Callbacks will run onto ForkJoinPool.commonPool(), unless it does not
215+ * support a parallelism level of at least two, in which case a new Thread
216+ * is used.
217+ *
192218 * @return a CompletionStage that runs all callbacks asynchronously and does
193219 * not support the CompletableFuture interface
194220 */
195221 def toJava : CompletionStage [T ] = FutureConverters .toJava(__self)
222+
223+ /**
224+ * Returns a CompletionStage that will be completed with the same value or
225+ * exception as the given Scala Future when that completes. Since the Future is a read-only
226+ * representation, this CompletionStage does not support the
227+ * <code>toCompletableFuture</code> method. The semantics of Scala Future
228+ * demand that all callbacks are invoked asynchronously by default, therefore
229+ * the returned CompletionStage routes all calls to synchronous
230+ * transformations to their asynchronous counterparts, i.e.
231+ * <code>thenRun</code> will internally call <code>thenRunAsync</code>.
232+ *
233+ * @param e The Java Executor onto which run the callbacks
234+ * @return a CompletionStage that runs all callbacks asynchronously and does
235+ * not support the CompletableFuture interface
236+ */
237+ def toJava (e : Executor ): CompletionStage [T ] = FutureConverters .toJava(__self, e)
196238 }
197239
198240 implicit def CompletionStageOps [T ](cs : CompletionStage [T ]): CompletionStageOps [T ] = new CompletionStageOps (cs)
0 commit comments