2016-04-01 3 views
4

私は独自の「実行コンテキスト」抽象化(JavaのExecutorとおおよそ同じ)を提供するScalaを使用しています。私はExecutorServiceを必要とする別のJavaライブラリと対話したい。 Executorの周りにExecutorServiceラッパーを構築することは可能ですか?ExecutorからExecutorServiceを作成

私はExecutorServiceExecutorのサブクラスだと理解しています。しかし、私の場合、私は後者を持っていて、それから前者を構築する必要があります。

ExecutorServiceがシャットダウン/待機機能を提供していない場合は、それは問題ありません。私が本当に気にしているのは、submitの具体的な実装で、実装がexecuteの場合です。

答えて

2

Turning an ExecutionContext to an ExecutorService (or rather and ExecutorService AND an ExecutionContext) using Scala 2.10+

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 
    } 
    } 

それともWrapping a scala.concurrent.ExecutionContext into java.concurrent.ExecutorService最適です

+0

、感謝をしてみてください! – larsrh

関連する問題