Scala Study Notes (3) actor simple example

Source: Internet
Author: User

After learning about some basic features of Scala, we can see its thread implementation: actor. I don't know if this is appropriate, because actor seems to focus on using message transmission to collaborate between entities. The prototype is as follows:

abstract class Actor extends Thread with MailBox{def act() : Unitoverride def run(): Unit = act()def !(msg: Any) = send(msg)}

 

From the source code of this version (<= Scala 2.7), we can see that in addition to representing the Thread class, actor also introduces mailbox. We all know that it inherits Java. lang. thread only requires rewriting of the run () method, and there will be no other changes. Scala Uses Act () to replace the habit of using run, as mailbox is incorporated, scala actor has new methods such as receive and receivewith to complete message delivery collaboration. (However, I recently found that all these changes have taken place in the new Scala version. The actor is no longer mixed into mailbox, and the entire actor only contains the object associated with it.
The definition of actor and the abstract interface trait actor seems that the role implemented by the thread is more concise and clear. Receive and receivewithin are directly added to the companion object actor and trait actor .)

 

Here we will not discuss the features of actor in depth. Let's take a small example to see how actor works:

import scala.actors.Actor, java.util._abstract class AuctionMessagecase class Offer(bid: Int, client: Actor) extends AuctionMessagecase class Inquire(client: Actor) extends AuctionMessagecase object TIMEOUTabstract class AuctionReplycase class Status(asked: Int, expire: Date) extends AuctionReplycase object BestOffer extends AuctionReplycase class BeatenOffer(maxBid: Int) extends AuctionReplycase class AuctionConcluded(seller: Actor, client: Actor) extends AuctionReplycase object AuctionFailed extends AuctionReplycase object AuctionOver extends AuctionReply/*** Before finally stopping, it stays active for another period determined by the* timeToShutdown constant and replies to further offers that the auction is closed**/class Auction (seller: Actor, minBid: Int, closing: Date) extends Actor{// 60 minutes to shut down the auctionval timeToShutdown = 3600000// minimum bid for each offerval bidIncrement = 10def act(){var maxBid = minBid - bidIncrementvar maxBidder: Actor = nullvar running = truewhile(running){//receiveWithin: just span a period of time for mailbox messages then stoppedreceiveWithin ((closing.getTime() - new Date().getTime())){case Offer(bid, client) =>if(bid >= maxBid + bidIncrement){//beat successfully, notify the current maxBidder, then replace itif(maxBid >= minBid) maxBidder ! BeatenOffer(bid)//reply to client the current offer peak valuemaxBid = bid;maxBidder = client; client ! BestOffer}else{//beat failed, return the current max bid value to offer clientclient ! BeatenOffer(maxBid)}case Inquire(client) =>// query the max bid and closing timeclient ! Status(maxBid, closing)case TIMEOUT =>//auction doneif(maxBid >= minBid){val reply = AuctionConcluded(seller, maxBidder)maxBidder ! reply; seller ! reply}else{//no one get the auction, notify sellerseller ! AuctionFailed}//for further offer message, tell them overreceiveWithin(timeToShutdown){case Offer(_, client) => client ! AuctionOvercase TIMEOUT => running = false}}}}}

This is an example of an auction communication. In the while loop, auction can distinguish the types of request messages and then handle them appropriately. When a timeout event occurs, the entire end process starts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.