0

When I start some Scala Futures that perform expensive computations, the main thread starts to lag very badly. I don't use any callbacks just plain:

//got global execution context
for(i <- 0 until NUMBER_OF_FUTURES){
   Future{do_work()}
}

What causes the freezes ?

EDIT: Maybe execution context allows to run Futures on the main thread ?

EDIT2 Execution context I'm using:

private implicit val ec = new ExecutionContext
{
   val threadPool = Executors.newSingleThreadExecutor()

   def execute(runnable: Runnable) {
     threadPool.submit(runnable)
   }

   def reportFailure(t: Throwable) {}
}
Rulli
  • 35
  • 4
  • Does the whole system slow down or is it just the main thread? Does the main thread need to do any work to set up the computations before they even start? – Kim Stebel Aug 20 '16 at 16:33
  • @KimStebel Not sure about the whole system. No the main thread just invokes the worker threads. – Rulli Aug 20 '16 at 16:37
  • @KimStebel The main thread sets things up before using futures. – Rulli Aug 20 '16 at 16:38
  • Well, if the whole system is under heavy load, that slows down the main thread as well. – Kim Stebel Aug 20 '16 at 16:40
  • @KimStebel How can I force those computations not to interrupt main's thread business ? I want something similar to general purpose GPU programming, that is fully asynchronous and doesn't slow down the CPU , here using multithreading – Rulli Aug 20 '16 at 16:45
  • @KimStebel And really how can a thread running on its own CPU core slow down a thread running on another core ? It doesn't make any sense – Rulli Aug 20 '16 at 16:50
  • Because there are more threads than CPU cores, they need to be scheduled. You can raise the priority of the main thread, but it's a complex issue and this might not work in every situation. See http://stackoverflow.com/questions/1617963/setting-priority-to-javas-threads – Kim Stebel Aug 20 '16 at 16:52
  • @KimStebel setting priority did not help. I think threads are scheduled by default – Rulli Aug 20 '16 at 16:55
  • 1
    Are you using the `global` `ExecutionContext`? Can you try running this with a different `ExecutionContext` that doesn't try using up all the processors? E.g., if you have 4 processors try using `concurrent.ExecutionContext.fromExecutor(java.util.concurrent.Executors.newFixedThreadPool(3))` – Kolmar Aug 20 '16 at 17:32
  • @Kolmar I do this already using a custom one. I tried both the fixed pool and single thread pool – Rulli Aug 20 '16 at 17:58
  • Try measuring the performance with *ScalaMeter*; it might just be the case of JVM overhead for concurrent tasks. You could try *ViusalVM* as well so we could see what's happening there. – sebszyller Aug 20 '16 at 18:27
  • @sebszyller I tested this with VisualVM it tells that the main thread is active at all times. Tested with single thread pool. But I still get lag on the main thread. Do you want a snapshot file ? – Rulli Aug 20 '16 at 19:15
  • @sebszyller GC activity is stable zero – Rulli Aug 20 '16 at 19:16

0 Answers0