Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8990563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:31:39+00:00 2026-06-15T22:31:39+00:00

I am trying to design a dispatcher-worker actor pattern for Scala using the standard

  • 0

I am trying to design a dispatcher-worker actor pattern for Scala using the standard scala.actors package.

The dispatcher receives work from a java.util.concurrent.LinkedBlockingQueue and sends it out to worker actors to be processed. When all of the work is done the dispatcher should tell each of the workers to quit and then it should also quit. Here is the code I came up with, but it hangs when all of the work is done (I think there are pending 'GiveMeWork messages in the dispatcher’s queue):

import java.util.concurrent.LinkedBlockingQueue
import scala.actors.Actor

object Dispatcher
extends Actor {
  println("Dispatcher created")

  def act() {
    val workers = (1 to 4).map(id => (new Worker(id)).start())

    loop {
      react {
        case 'GiveMeWork =>
          // println("Worker asked for work")
          val (time, i) = workQueue.take()
          if (time == 0) {
            println("Quitting time")
            workers.foreach(_ !? 0L)
          } else {
            println("Arrival at dispatcher: i: " + i + " dispatch time: " +
                    time + ", elapsed: " + (System.nanoTime() - time))
            sender ! time
          }
        case 'Quit =>
          println("Told to quit")
          sender ! 'OffDuty
          exit()
      }
    }
  }
}

class Worker(id: Int)
extends Actor {
  println("Worker(" + id + ") created")
  var jobs = 0

  def act() {
    Dispatcher ! 'GiveMeWork

    loop {
      react {
        case time: Long =>
          if (time == 0) {
            println("Worker(" + id + ") completed " + jobs + " jobs")
            sender ! 'OffDuty
            exit()
          } else {
            println("Arrival at worker(" + id + "): dispatch time: " +
                    time + ", elapsed: " + (System.nanoTime() - time))
            Thread.sleep(id)
            jobs += 1
            Dispatcher ! 'GiveMeWork
          }
      }
    }
  }
}

val workQueue = new LinkedBlockingQueue[(Long, Int)](1000)

Dispatcher.start()

for (i <- 0 until 5000) {
  Thread.sleep(1)
  workQueue.put((System.nanoTime(), i))
}

workQueue.put((0L, 0))

println("Telling Dispatcher to quit")
Dispatcher !? 'Quit
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:31:40+00:00Added an answer on June 15, 2026 at 10:31 pm

    There is a race:

    val (time, i) = workQueue.take()
    

    All the work is done, including workQueue.put((0L, 0)), so it will wait forever.

    It’s a bad idea to use different types of concurrency simultaneously.

    Dispatcher can inform task source about task limit:

    import scala.actors.{Actor, OutputChannel}
    import scala.collection.mutable.Queue
    
    case class Task(time: Long, i: Int)
    case object GiveMeWork
    case object Quit
    case object OffDuty
    
    object Dispatcher extends Actor {
      println("Dispatcher created")
    
      def act() {
        val workers = (1 to 4).map(id => (new Worker(id)).start())
        val waitingWorkers = Queue[OutputChannel[Any]](workers: _*)
        val tasks = Queue[Task]()
        var workSender: Option[OutputChannel[Any]] = None
    
        loop {
          react {
            case GiveMeWork =>
              if (!tasks.isEmpty) sender ! tasks.dequeue()
              else waitingWorkers enqueue sender
    
              workSender map { _ ! GiveMeWork }
              workSender = None
            case t: Task =>
              if (!waitingWorkers.isEmpty) waitingWorkers.dequeue() ! t
              else tasks enqueue t
    
              if (tasks.length < 1000) sender ! GiveMeWork
              else workSender = Some(sender)
            case Quit =>
              println("Told to quit")
              workers.foreach{ _ ! Quit }
              sender ! OffDuty
              exit()
          }
        }
      }
    }
    
    class Worker(id: Int)
    extends Actor {
      var jobs = 0
    
      def act() {
        loop {
          react {
            case t: Task =>
              Thread.sleep(id)
              jobs += 1
              Dispatcher ! GiveMeWork
            case Quit =>
              println("Worker(" + id + ") completed " + jobs + " jobs")
              sender ! OffDuty
              exit()
          }
        }
      }
    }
    
    Dispatcher.start()
    
    for (i <- 0 until 5000) {
      Thread.sleep(1)
      Dispatcher !? Task(System.nanoTime(), i)
    }
    
    println("Telling Dispatcher to quit")
    Dispatcher !? Quit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to design a UI in C# . I come from Java
I'm trying to design my app's interface in IB using a storyboard and have
I am trying to design a view using asp.net mvc 3 RC. I am
I'm trying to work with the LongListSelector control from the WP7 Silverlight Toolkit. It's
I'm trying to design a two factor authentication system (on PHP) using SMS as
I'm trying to design a lightweight way to store persistent data in Java. I've
i am trying to design an app on the basis of a java project
i'm trying to design interface like this one http://www.softpedia.com/screenshots/FlashFXP_2.png i'm using the QT design
I'm trying to design the architecture for a new LOB MVVM project utilising Caliburn
I am trying to design a listview with 3 textViews within it. 1 textview

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.