当前位置: 首页>>代码示例>>Scala>>正文


Scala Actor类代码示例

本文整理汇总了Scala中scala.actors.Actor的典型用法代码示例。如果您正苦于以下问题:Scala Actor类的具体用法?Scala Actor怎么用?Scala Actor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Actor类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: links

//设置package包名称以及导入依赖的类
package examples.actors

import scala.actors.{Actor, Exit}
import scala.actors.Actor._

object links extends App {

  case object Stop

  actor {
    val start = link(p(2))
    start ! Stop
  }

  def p(n: Int): Actor =
    if (n == 0) top1()
    else top(p(n-1), n)

  def top(a: Actor, n: Int): Actor = actor {
    println("starting actor " + n + " (" + self + ")")
    self.trapExit = true
    link(a)
    loop {
      receive {
        case ex @ Exit(from, reason) =>
          println("Actor " + n + " received " + ex)
          exit('finished)
        case any => {
          println("Actor " + n + " received " + any)
          a ! any
        }
      }
    }
  }

  def top1(): Actor = actor {
    println("starting last actor"  + " (" + self + ")")
    receive {
      case Stop =>
        println("Last actor now exiting")
        exit('abnormal)
      case any =>
        println("Last actor received " + any)
        top1()
    }
  }
} 
开发者ID:dschatzl,项目名称:spark-delay-scheduling,代码行数:48,代码来源:links.scala

示例2: WorkItem

//设置package包名称以及导入依赖的类
package simulation

import scala.actors.Actor

case class WorkItem(time: Int, msg: Any, target: Actor)
case class AfterDelay(delay: Int, msg: Any, target: Actor)

case object Start
case object Stop

trait Simulant extends Actor {
  val clock: Clock
  def handleMessage(msg: Any)
  def simStarting() {}
  def act() {
    loop {
      react {
        case Stop => exit()
        case Ping(time) =>
          if (time == 1) simStarting()
          clock ! Pong(time, this)
        case msg => handleMessage(msg)
      }
    }
  }

  start()
} 
开发者ID:mhotchen,项目名称:programming-in-scala,代码行数:29,代码来源:Simulant.scala

示例3: message

//设置package包名称以及导入依赖的类
package examples.actors

import scala.actors.{Actor, Scheduler}
import scala.actors.Actor._
import scala.actors.scheduler.SingleThreadedScheduler

object message {
  def main(args: Array[String]) {
    val n = try { args(0).toInt }
    catch {
      case _: Throwable =>
        println("Usage: examples.actors.message <n>")
        sys.exit
    }
    val nActors = 500
    val finalSum = n * nActors
    Scheduler.impl = new SingleThreadedScheduler

    def beh(next: Actor, sum: Int) {
      react {
        case value: Int =>
          val j = value + 1; val nsum = sum + j
          if (next == null && nsum >= n * j)
            println(nsum)
          else {
            if (next != null) next ! j
            if (nsum < n * j) beh(next, nsum)
          }
      }
    }

    def actorChain(i: Int, a: Actor): Actor =
      if (i > 0) actorChain(i-1, actor(beh(a, 0))) else a

    val firstActor = actorChain(nActors, null)
    var i = n; while (i > 0) { firstActor ! 0; i -= 1 }

    Scheduler.shutdown()
  }
} 
开发者ID:chenc10,项目名称:spark-SSR-ICDCS17,代码行数:41,代码来源:message.scala

示例4: Ping

//设置package包名称以及导入依赖的类
package examples.actors

import scala.actors.Actor
import scala.actors.Actor._

case object Ping
case object Pong
case object Stop


object pingpong extends App {
  val pong = new Pong
  val ping = new Ping(100000, pong)
  ping.start
  pong.start
}

class Ping(count: Int, pong: Actor) extends Actor {
  def act() {
    var pingsLeft = count - 1
    pong ! Ping
    loop {
      react {
        case Pong =>
          if (pingsLeft % 1000 == 0)
            println("Ping: pong")
          if (pingsLeft > 0) {
            pong ! Ping
            pingsLeft -= 1
          } else {
            println("Ping: stop")
            pong ! Stop
            exit()
          }
      }
    }
  }
}

class Pong extends Actor {
  def act() {
    var pongCount = 0
    loop {
      react {
        case Ping =>
          if (pongCount % 1000 == 0)
            println("Pong: ping "+pongCount)
          sender ! Pong
          pongCount += 1
        case Stop =>
          println("Pong: stop")
          exit()
      }
    }
  }
} 
开发者ID:chenc10,项目名称:spark-SSR-ICDCS17,代码行数:57,代码来源:pingpong.scala

示例5: ParallelRunner

//设置package包名称以及导入依赖的类
package main

import scala.actors.Actor


class ParallelRunner(query: String, k: Int, d: Int, clockStart: Long) extends Actor {

  override def act(): Unit = {
    if (query == "q1") {
      val q1 = new Query1
      q1.run(clockStart)
    }
    else {
      val q2 = new Query2
      q2.run(k, d, clockStart)
    }
  }
} 
开发者ID:cvlas,项目名称:DEBS2016GC,代码行数:19,代码来源:ParallelRunner.scala

示例6: ControlCenter

//设置package包名称以及导入依赖的类
package com.remark.lunarrover

import scala.actors.Actor

/**
  * Created by xinruyi on 2017/3/15.
  */
/**
  * control center for manipulating rovers
  */
object ControlCenter extends Actor{

  override def act(): Unit = {
    while (true){
//      Thread.sleep(500)
      receive{
        case MessageCoorCorrect(roverIdx, start, end, time) =>{
          printf("%s ?LunarRover-%s Coordinate_Correct? Received Position [%s:%s]  Target Position [%s:%s]  \n",
            time, roverIdx, start.x, start.y, end.x, end.y)
        }
        case MessageStart(roverIdx, start, nowPosition, end, speed, time) =>{
          val prePosition = Utils.forecast(nowPosition, speed)
          printf("%s ?LunarRover-%s Start? Received Position [%s:%s] Predicted Position [%s:%s] Target Position [%s:%s] Speed:%s \n",
            time, roverIdx, nowPosition.x, nowPosition.y, prePosition.x, prePosition.y, end.x, end.y, speed)
        }
        case MessageMove(roverIdx, start, nowPosition, end, speed, angle, time) =>{
          val prePosition = Utils.forecast(nowPosition, speed, angle)
          printf("%s ?LunarRover-%s Move? Received Position [%s:%s] Predicted Position [%s:%s] Target Position [%s:%s] Speed:%s \n",
            time, roverIdx, nowPosition.x, nowPosition.y, prePosition.x, prePosition.y, end.x, end.y, speed)
        }
        case MessageTurn(roverIdx, start, nowPosition, end, speed, angle, time) =>{
          val prePosition = Utils.forecast(nowPosition, speed)
          printf("%s ?LunarRover-%s Turn? Received Position [%s:%s] Predicted Position [%s:%s] Target Position [%s:%s] Speed:%s \n",
            time, roverIdx, nowPosition.x, nowPosition.y, prePosition.x, prePosition.y, end.x, end.y, speed)
        }
        case MessageStop(roverIdx, start, nowPosition, end, time) =>{
          val prePosition = Utils.forecast(nowPosition, 0)
          printf("%s ?LunarRover-%s Stop? Received Position [%s:%s] Predicted Position [%s:%s] Target Position [%s:%s] Speed:%s \n",
            time, roverIdx, nowPosition.x, nowPosition.y, prePosition.x, prePosition.y, end.x, end.y, 0)
        }
      }
    }
  }
} 
开发者ID:KinusHsin,项目名称:Remark,代码行数:45,代码来源:ControlCenter.scala

示例7: SyncMsgAndAyscMsg

//设置package包名称以及导入依赖的类
package actor

import scala.actors.Actor


class SyncMsgAndAyscMsg extends Actor {
  override def act(): Unit = {

    loop(
      react {
        case "start" => println("start")
        case SyncMsg(id, msg) => {
          println(s"id:$id , SyncMsg:$msg")
          sender ! ReplyMsg(1,"finished")
        }
      }
    )
  }
}


case class SyncMsg(id: Int, msg: String) //????

case class AyncMsg(id: Int, msg: String)

case class ReplyMsg(id: Int, msg: String)

object AppRun extends App {

  val msg: SyncMsgAndAyscMsg = new SyncMsgAndAyscMsg
  msg.start() //start
  val replyMsg= msg !? SyncMsg(10000, "syncMsg")
  println(replyMsg)

  val retMsg = msg !! SyncMsg(11111,"syncMsg")
  println(retMsg)
  println(retMsg.isSet)
  val c = retMsg.apply
  println(retMsg.isSet)
  println(c)
} 
开发者ID:gomaster-me,项目名称:Scala_sidepro,代码行数:42,代码来源:SyncMsgAndAyscMsg.scala

示例8: ActorTest

//设置package包名称以及导入依赖的类
package actor


object ActorTest {

}

import scala.actors.Actor._ //??????
import scala.actors.Actor

object MyActor extends Actor {
  override def act() = {
    while(true) {
      receive {
        case msg : Double => println("myactor double, msg = " + msg)
        case _ => println("myactorunknow")
      }
    }
  }
}

object ActorMessages {

  val messageActor = actor {//??actor
    while(true){
      //???????'actor??'????????????????
      receive {
        //isDefinedAtapply
        //????isDefinedAt??receive??????????????????????????????apply??????????????????
        case msg : String => {
          println("string, msg = "+ msg)
        }
        case msg : Double => println("double, msg = " + msg)
        case _=> println("unknow")
      }
    }
  }

  def main(args:Array[String]): Unit = {

    //?actor????????
    messageActor ! "hadoop" //??actor????????
    messageActor! Math.PI
    messageActor! 0

    MyActor.start()//??????????
    MyActor !"spark"
  }
} 
开发者ID:gomaster-me,项目名称:Scala_sidepro,代码行数:50,代码来源:ActorTest.scala

示例9: T1

//设置package包名称以及导入依赖的类
package actor

import scala.actors.Actor


object T1 extends Actor {
  //Actor ?trait ??????act?? ??????? ctrl+i ?????
  override def act(): Unit = {
    for (elem <- 1 to 20) {
      println(s"actor-1: $elem")
      Thread.sleep(1000)
    }
  }
}

object T2 extends Actor{
  override def act(): Unit = {
    for (elem <- 1 to 20) {
      println(s"actor-2? $elem")
      Thread.sleep(1000)
    }
  }
}

object AppStart{
  def main(args: Array[String]) {
    T1.start()
    T2.start()
    println("main")
  }
} 
开发者ID:gomaster-me,项目名称:Scala_sidepro,代码行数:32,代码来源:TwoThreadPrint.scala

示例10: Server

//设置package包名称以及导入依赖的类
package actor.actor_mes

import scala.actors.Actor


object Server extends Actor {
  override def act(): Unit = {
    loop(react {
      case Person(name, age) => {
        println(s"name = $name ,age = $age")
        sender ! "copy that" //????????????
      }
      case _ => println("unknow") //?????????????actor???????????????????
    })
  }
} 
开发者ID:gomaster-me,项目名称:Scala_sidepro,代码行数:17,代码来源:Server.scala

示例11: ChromAnalysis

//设置package包名称以及导入依赖的类
package se.lth.immun.anubis

import scala.actors.Actor
import scala.actors.Actor._


import se.lth.immun.proteomics.PeptideIon

object ChromAnalysis {
	case class Chrom(pi:PeptideIon, aIn:AnubisInput)
	case class Peaks(pi:PeptideIon, peakList:PeakList)
	case class AnalysisError(pi:PeptideIon, msg:String)
}


class ChromAnalysis extends Actor {
	
    val pepQuant:PepQuant = new PepQuant()

	import ChromAnalysis._
	
	def act = {
		loop {
			react {
				case Chrom(pi, aIn) =>
					try {
						sender ! Peaks(pi, PeakList(aIn.fragments, pepQuant.findBestCandidate(aIn)))
					} catch {
						case e:Exception =>
							sender ! AnalysisError(pi, "Exception encountered during analysis of %s\n%s".format(pi, e.getStackTrace.mkString("\n")))
					}
			}
		}
	}
} 
开发者ID:fickludd,项目名称:anubis,代码行数:36,代码来源:ChromAnalysis.scala

示例12: Person3

//设置package包名称以及导入依赖的类
import scala.actors.Actor
import scala.actors.Actor._


case class Person3(name: String, ge: Int)

class HelloActor extends Actor{
  def act(): Unit ={
    while (true){
      receive{
        //case????????????Actor(self)
        case Person3(name,age) => {
          //?????,????????????,??Actor?????????????
          println("name :" + name +"age:"+age)
          //??????????Actor,??????????Actor
          sender ! "Echo!!!"
        }
      }
    }
  }
}

object Actor_CaseClass {
  def main(args: Array[String]) {
    val hiactor = new HelloActor
    hiactor.start
    hiactor ! Person3("variant",15)

    //self????,Actor??,??????
    self.receive{case msg => println(msg)}
  }
} 
开发者ID:WeiLi1201,项目名称:ProgrammingNote,代码行数:33,代码来源:Actor_CaseClass.scala

示例13: First_Actor

//设置package包名称以及导入依赖的类
import scala.actors.Actor



object First_Actor extends Actor{
  def act(): Unit ={
    println(Thread.currentThread().getName)
    for (i <- 1 to 10){
      println("Step:" + i)
      Thread.sleep(1000)
    }
  }
}

object Second_Actot extends Actor{
  def act(): Unit = {
    println(Thread.currentThread().getName)
    for (i <- 1 to 10){
      println("Step :" + i)
      Thread.sleep(1000)
    }
  }
}

object Hello_Actor {
  def main(args: Array[String]) {
    First_Actor.start
    Second_Actot.start
  }
} 
开发者ID:WeiLi1201,项目名称:ProgrammingNote,代码行数:31,代码来源:HelloActor.scala

示例14: Actor_Message

//设置package包名称以及导入依赖的类
import scala.actors.Actor._
import scala.actors.Actor

object Actor_Message extends Actor{
  def act(): Unit ={
    while(true){
      receive{
        case msg => println("Message content Actor from inbox:" + msg)
      }
    }
  }
}


object Actor_Messages {
  def main(args: Array[String]) {
    val actor_Message = actor{
      while (true){
        receive{
          case msg => println("Message content from inbox:" + msg)
        }
      }
    }

    val Double_Message = actor{
      while (true){
        //??receive???case,Actor????
        //???????,Actor???isDefinedAt??????
        //isDefinedAt ?????????????receive?case?????????
        //????true,??Apply??????,?????case
        //????false,Actor???????????,?????Apply??,?????
        receive{ //Apply | isDefinedAt
          case msg : Double=> println("Double number from inbox:" + msg)
        }
      }
    }

    Actor_Message.start
    Actor_Message ! "Hadoop"
    actor_Message ! "Spark"
    Double_Message ! Math.PI
    Double_Message ! "Hadoop 2"



  }
} 
开发者ID:WeiLi1201,项目名称:ProgrammingNote,代码行数:48,代码来源:Actor_Message.scala


注:本文中的scala.actors.Actor类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。