Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Scala Actor multithreading

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to understand Scala Actor multithreading". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Scala Actor is the basis of multithreading in Scala. The core idea is to use message passing to share and synchronize information between threads.

The Scala Actor thread model can be understood like this: all Actor share a thread pool, and the total number of threads can be configured or determined according to the number of CPU; when an Actor starts, Scala assigns a thread to it, and if the receive model is used, the thread is always owned by the Actor. If the react model is used, the Scala throws an exception after executing the react method, then the thread can be used by other Actor.

Let's take a look at some core code.

Def start (): Actor = synchronized {/ / Reset various flags. / Note that we do * not* reset `trapExit`. The reason is that / / users should be able to set the field in the constructor / / and before `act` is called. ExitReason = 'normal exiting = false shouldExit = false scheduler execute {ActorGC.newActor (Actor.this) (new Reaction (Actor.this)) .run ()} this}

Reaction implements the Runnable interface, and scheduler is basically a thread pool, so after calling the start method, there will be a thread to serve the Actor.

Use the receive model.

Def receive [R] (f: PartialFunction [Any, R]): r = {assert (Actor.self = = this) "receive from channel belonging to other actor") this.synchronized {if (shouldExit) exit () / / links val qel = mailbox.extractFirst ((m: Any) = > f.isDefinedAt (m) if (null eq qel) {waitingFor = f.isDefinedAt isSuspended = true suspendActor ()} else {received = Some (qel.msg) sessions = qel.session:: sessions} waitingFor = waitingForNone isSuspended = False} val result = f (received.get) sessions = sessions.tail result

If there is no message in the current mailbox, call suspendActor, and the method will call wait;. If there is a message, this will call PartialFunction for processing.

Use the react model.

Def react (f: PartialFunction [Any, Unit]): Nothing = {assert (Actor.self = = this, "react on channel belonging to other actor") this.synchronized {if (shouldExit) exit () / / links val qel = mailbox.extractFirst ((m: Any) = > f.isDefinedAt (m) if (null eq qel) {waitingFor = f.isDefinedAt continuation = f isDetached = true} else {sessions = List (qel.session) scheduleActor (f) Qel.msg)} throw new SuspendActorException}

If there is no message that can be processed in the current mailbox, set waitingFor and continuation, these two variables will be used when the message is received; if there is a message, call scheduleActor, and the method will select a new thread in the thread pool to process, and the specific processing method is also determined by PartialFunction. No matter which path the react returns immediately, or immediately throws an exception to end the execution of the thread, so that the thread can be used by other Actor.

Let's take a look at the processing code that receives the message.

Def send (msg: Any ReplyTo: OutputChannel [Any] = synchronized {if (waitingFor (msg)) {received = Some (msg) if (isSuspended) sessions = replyTo:: sessions else sessions = List (replyTo) waitingFor = waitingForNone if (! onTimeout.isEmpty) {onTimeout.get.cancel () onTimeout = None} if (isSuspended) resumeActor () else / / assert continuation! = null scheduler.execute (new Reaction (this, continuation) Msg)} else {mailbox.append (msg, replyTo)}

If the message is not currently waiting or the received message cannot be processed, it is thrown into the mailbox; instead, the message is processed. There are branches for the receive model and the react model: if the isSuspended is true, which means the receive model, and the thread is in wait, call resumeActor, the method will call notify; otherwise it is the react model, and also select a thread in the thread pool to process.

This is the end of "how to understand Scala Actor multithreading". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report