Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save viktorklang/5245161 to your computer and use it in GitHub Desktop.
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+
/*
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
}
}
}
@ErunamoJAZZ
Copy link

It is very useful. I used this to get an ExecutorService to use with vavr's Futures in Java.
Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment