Executing the following code reports NoClassDefFoundError's occasionally. And It seem more likely the error is thrown with a higher upperbound. What's going wrong here?
Exception in thread "pool-1-thread-1" java.lang.NoClassDefFoundError: Could not initialize class scala.util.control.NonFatal$
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:29)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Here is the code:
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
import java.util.concurrent.ArrayBlockingQueue
import ThreadPoolExecutor._
import scala.concurrent._
import scala.concurrent.duration._
implicit val context = ExecutionContext.fromExecutorService(
new ThreadPoolExecutor(
5, 5,
0L, TimeUnit.SECONDS,
new ArrayBlockingQueue[Runnable](5),
new CallerRunsPolicy
)
)
def reduce(a: Future[Long], b: Future[Long]) = {
a.flatMap(v => b.map(v + _))
}
val upperbound = 100000
val sum = (1 to upperbound).toIterator.map(i => future(i.toLong)).reduce(reduce)
println(Await.result(sum, Duration.Inf))
context.shutdown