Quantcast

Scala Actors Starvation

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Scala Actors Starvation

Vincent Marquez
Hello all, I've been playing around with Scala for a bit now, and recently (last night) upgraded to the new version of the Eclipse plugin for 2.8.  I've noticed something changed in some of my code, notably, Actors are now 'starving' in certain scenarios and not continuing to run as expected.

Now, I understand *why* its happening, but I'm not sure it is the correct behavior.  It appears that only a certain number of threads are started for use by Actors, and once all of those threads are 'locked up', no more will be created.  Here is a test case very similar to the one I've encountered: http://pastebin.com/Q32i03wL  (and also pasted below).

I'm using a latch to wait on another actor to finish its job, which will then release the latch, but its deadlocking when I have more parallel stuff going on then I do threads in the actor thread pool.  Is this a bug, or the correct behavior?  Thanks in advance for any replies.  
------------------------------------------------------------------------------------------------------------------------------------------
import scala.actors.Actor
import scala.actors._
import scala.actors.Actor._
import java.util.concurrent.CountDownLatch

object ScalaActorStarvation {
   def main(args: Array[String]) {
 val iterations = 30
  for (i <- 1 to iterations) {
  new ParallelTesting() ! ""+i
   }
   }
}
   

class DoStuffActor() extends Actor {
start()
def act() {
loop {  react {
case f:(()=>Unit) => System.err.println("do some async shit")
Thread.sleep(100)
           f() 
           
      
     }}  
     }
}

class ParallelTesting extends Actor  {
start()
   
def act() {
loop {  react {
case str:String => val latch = new CountDownLatch(1)
new DoStuffActor() ! ( ()=>{ println("someCode " + str)
        latch.countDown()
        } ) 
    latch.await()
    this.exit
 }
            }
  }
}


------------------------------------------------------------------------
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Scala Actors Starvation

Philipp Haller-2
On 07/08/2010 01:39 AM, Vincent Marquez wrote:

> Hello all, I've been playing around with Scala for a bit now, and
> recently (last night) upgraded to the new version of the Eclipse plugin
> for 2.8.  I've noticed something changed in some of my code, notably,
> Actors are now 'starving' in certain scenarios and not continuing to run
> as expected.
>
> Now, I understand *why* its happening, but I'm not sure it is the
> correct behavior.  It appears that only a certain number of threads are
> started for use by Actors, and once all of those threads are 'locked
> up', no more will be created.  Here is a test case very similar to the
> one I've encountered: http://pastebin.com/Q32i03wL  (and also pasted below).
>
> I'm using a latch to wait on another actor to finish its job, which will
> then release the latch, but its deadlocking when I have more parallel
> stuff going on then I do threads in the actor thread pool.  Is this a
> bug, or the correct behavior?  Thanks in advance for any replies.

The starvation detection mechanism in Scala 2.7 was too expensive to
support in the new default scheduler. However, there is a starvation
detection mechanism for thread-blocking methods of the `Actor` object
(`receive` etc.).

For handling arbitrary blocking methods, I outlined several solutions in
a recent thread on scala-user:
http://article.gmane.org/gmane.comp.lang.scala.user/28381/

Cheers,
Philipp

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Scala Actors Starvation

Vincent Marquez
Thanks again for your reply.  It looks like in my case, the solution is to use a different scheduler, is there any documentation on how I'd go about using ResizableThreadPoolScheduler with my actors instead of the normal Scheduler?  

I'd also like to add that in my very humble opinion, the 'default' scheduler should be the most *correct* behaving one, not the most performant. If Scala is going to pitch actors/react as 'microthreads', its a bit weird that they can starve so easily using the default scheduler. .  I'd much prefer having to do more work/configuration to get the better performing but more limiting thread pool scheduler when I need it.  Again, this is just my opinion.  

--Vincent
  

On Thu, Jul 8, 2010 at 1:41 AM, Philipp Haller <[hidden email]> wrote:
On 07/08/2010 01:39 AM, Vincent Marquez wrote:
Hello all, I've been playing around with Scala for a bit now, and
recently (last night) upgraded to the new version of the Eclipse plugin
for 2.8.  I've noticed something changed in some of my code, notably,
Actors are now 'starving' in certain scenarios and not continuing to run
as expected.

Now, I understand *why* its happening, but I'm not sure it is the
correct behavior.  It appears that only a certain number of threads are
started for use by Actors, and once all of those threads are 'locked
up', no more will be created.  Here is a test case very similar to the
one I've encountered: http://pastebin.com/Q32i03wL  (and also pasted below).

I'm using a latch to wait on another actor to finish its job, which will
then release the latch, but its deadlocking when I have more parallel
stuff going on then I do threads in the actor thread pool.  Is this a
bug, or the correct behavior?  Thanks in advance for any replies.

The starvation detection mechanism in Scala 2.7 was too expensive to support in the new default scheduler. However, there is a starvation detection mechanism for thread-blocking methods of the `Actor` object (`receive` etc.).

For handling arbitrary blocking methods, I outlined several solutions in a recent thread on scala-user:
http://article.gmane.org/gmane.comp.lang.scala.user/28381/

Cheers,
Philipp


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Scala Actors Starvation

SXPCrazy
I think you can use the below code to setup Scheduler use a ResizableThreadPoolScheduler as new Actor's Scheduler: 

System.setProperty("actors.enableForkJoin", "false")



On Fri, Jul 9, 2010 at 2:26 AM, Vincent Marquez <[hidden email]> wrote:
Thanks again for your reply.  It looks like in my case, the solution is to use a different scheduler, is there any documentation on how I'd go about using ResizableThreadPoolScheduler with my actors instead of the normal Scheduler?  

I'd also like to add that in my very humble opinion, the 'default' scheduler should be the most *correct* behaving one, not the most performant. If Scala is going to pitch actors/react as 'microthreads', its a bit weird that they can starve so easily using the default scheduler. .  I'd much prefer having to do more work/configuration to get the better performing but more limiting thread pool scheduler when I need it.  Again, this is just my opinion.  

--Vincent
  

On Thu, Jul 8, 2010 at 1:41 AM, Philipp Haller <[hidden email]> wrote:
On 07/08/2010 01:39 AM, Vincent Marquez wrote:
Hello all, I've been playing around with Scala for a bit now, and
recently (last night) upgraded to the new version of the Eclipse plugin
for 2.8.  I've noticed something changed in some of my code, notably,
Actors are now 'starving' in certain scenarios and not continuing to run
as expected.

Now, I understand *why* its happening, but I'm not sure it is the
correct behavior.  It appears that only a certain number of threads are
started for use by Actors, and once all of those threads are 'locked
up', no more will be created.  Here is a test case very similar to the
one I've encountered: http://pastebin.com/Q32i03wL  (and also pasted below).

I'm using a latch to wait on another actor to finish its job, which will
then release the latch, but its deadlocking when I have more parallel
stuff going on then I do threads in the actor thread pool.  Is this a
bug, or the correct behavior?  Thanks in advance for any replies.

The starvation detection mechanism in Scala 2.7 was too expensive to support in the new default scheduler. However, there is a starvation detection mechanism for thread-blocking methods of the `Actor` object (`receive` etc.).

For handling arbitrary blocking methods, I outlined several solutions in a recent thread on scala-user:
http://article.gmane.org/gmane.comp.lang.scala.user/28381/

Cheers,
Philipp



Loading...