Last active
June 27, 2022 11:38
-
-
Save viktorklang/5245161 to your computer and use it in GitHub Desktop.
Turning an ExecutionContext to an ExecutorService (or rather and ExecutorService AND an ExecutionContext) using Scala 2.10+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2013 Viktor Klang | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
import scala.concurrent.{ExecutionContext, ExecutionContextExecutorService} | |
import java.util.concurrent.{ AbstractExecutorService, TimeUnit } | |
import java.util.Collections | |
object ExecutionContextExecutorServiceBridge { | |
def apply(ec: ExecutionContext): ExecutionContextExecutorService = ec match { | |
case null => throw null | |
case eces: ExecutionContextExecutorService => eces | |
case other => new AbstractExecutorService with ExecutionContextExecutorService { | |
override def prepare(): ExecutionContext = other | |
override def isShutdown = false | |
override def isTerminated = false | |
override def shutdown() = () | |
override def shutdownNow() = Collections.emptyList[Runnable] | |
override def execute(runnable: Runnable): Unit = other execute runnable | |
override def reportFailure(t: Throwable): Unit = other reportFailure t | |
override def awaitTermination(length: Long,unit: TimeUnit): Boolean = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is very useful. I used this to get an
ExecutorService
to use with vavr's Futures in Java.Thank you very much.