@@ -17,7 +17,7 @@ import scala.language.implicitConversions
17
17
import scala .concurrent .java8 .FuturesConvertersImpl ._
18
18
import scala .concurrent .java8 .FuturesConvertersImplCompat ._
19
19
import scala .concurrent .{ Future , Promise , ExecutionContext , ExecutionContextExecutorService , ExecutionContextExecutor }
20
- import java .util .concurrent .{ CompletionStage , Executor , ExecutorService }
20
+ import java .util .concurrent .{ CompletionStage , Executor , ExecutorService , ForkJoinPool }
21
21
import java .util .function .Consumer
22
22
23
23
/**
@@ -59,16 +59,38 @@ object FutureConverters {
59
59
* transformations to their asynchronous counterparts, i.e.
60
60
* <code>thenRun</code> will internally call <code>thenRunAsync</code>.
61
61
*
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
+ *
62
66
* @param f The Scala Future which may eventually supply the completion for
63
67
* the returned CompletionStage
64
68
* @return a CompletionStage that runs all callbacks asynchronously and does
65
69
* not support the CompletableFuture interface
66
70
*/
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 ] = {
68
90
f match {
69
91
case p : P [T @ unchecked] => p.wrapped
70
92
case _ =>
71
- val cf = new CF [T ](f)
93
+ val cf = new CF [T ](f, e )
72
94
implicit val ec = InternalCallbackExecutor
73
95
f onComplete cf
74
96
cf
@@ -189,10 +211,30 @@ object FutureConverters {
189
211
* transformations to their asynchronous counterparts, i.e.
190
212
* <code>thenRun</code> will internally call <code>thenRunAsync</code>.
191
213
*
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
+ *
192
218
* @return a CompletionStage that runs all callbacks asynchronously and does
193
219
* not support the CompletableFuture interface
194
220
*/
195
221
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)
196
238
}
197
239
198
240
implicit def CompletionStageOps [T ](cs : CompletionStage [T ]): CompletionStageOps [T ] = new CompletionStageOps (cs)
0 commit comments