Scala學習筆記(三)Actor簡單例子

來源:互聯網
上載者:User

在瞭解了Scala的一些基本特性之後,看到了它的線程實現:Actor。筆者不知道這麼叫是否合適,因為Actor的目的似乎專註於在實體之間使用訊息傳遞來協作。翻了一下它的原型定義如下:

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

 

從這個版本的源碼(<=Scala 2.7)中可以看出,Actor除了代表線程類之外還引入了MailBox,本來我們都知道繼承java.lang.Thread只會要求重寫run()方法,不會有別的改變,Scala使用act()來代替了使用run()的習慣,由於混入了MailBox使得Scala的Actor具有了receive和receiveWith等新的方法,從而完成訊息傳遞的協作。(但筆者最近在新的Scala版本中發現這一切都有了重大改變,Actor不再混入MailBox,整個Actor只包含了伴生對象object
Actor和抽象介面trait Actor的定義,看起來線程實現的角色更加簡潔明了。receive和receiveWithIn被同時直接加入為伴生對象Actor和trait Actor中。)

 

這裡不深入討論Actor的特點,先通過一個小例子,來看Actor是如何工作的:

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}}}}}

這是一個拍賣通訊的例子,在while迴圈裡Auction能分辨請求訊息的種類然後做合適的處理,當發生逾時事件,開始進行整個結束流程。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.