Actor "turn"?

In Java, to write a library that makes requests to the server, I usually implement a kind of dispatcher (as opposed to found in the Twitter4J library here: http://github.com/yusuke/twitter4j/blob/master/twitter4j-core/src /main/java/twitter4j/internal/async/DispatcherImpl.java ) to limit the number of connections, perform asynchronous tasks, etc.

The idea is that N threads are created. The "task" is placed in the queue, and all threads are notified, and one of the threads, when it is ready, will pull the item out of the queue, do the work and return to the standby state. If all threads are busy with work on a task, then the task is simply queued, and the next available thread will accept it.

This supports the maximum number of connections to N and allows you to simultaneously work no more than N tasks.

I am wondering which system I can create with Actors who will do the same thing? Is there a way to have N number of Actors, and when the new message is ready, send it to the Actor to process it - and if all the Actors are busy, just leave the message in the queue?

+3
source share
2 answers

Akka Framework , , ,

document - ( , , , ..), . .

. , :

  val workStealingDispatcher = Dispatchers.newExecutorBasedEventDrivenWorkStealingDispatcher("pooled-dispatcher")
  workStealingDispatcher
  .withNewThreadPoolWithLinkedBlockingQueueWithUnboundedCapacity
  .setCorePoolSize(16)
  .buildThreadPool

, :

class MyActor extends Actor {

    messageDispatcher = workStealingDispatcher

    def receive = {
      case _ =>
    }
  }

, 2+ , () (, , "" , ).

+4

, , 1 1 . , , - . , , . , , , , .

, , , . , . :

val q: Queue[AnyRef] = new Queue[AnyRef]
loop {
  react {
    case Enqueue(d) => q enqueue d
    case Dequeue(a) if q.nonEmpty => a ! (q dequeue)
    }
}
+1

Source: https://habr.com/ru/post/1748930/


All Articles