本文整理汇总了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()
}
}
}
示例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()
}
示例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()
}
}
示例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()
}
}
}
}
示例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)
}
}
}
示例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)
}
}
}
}
}
示例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)
}
示例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"
}
}
示例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")
}
}
示例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???????????????????
})
}
}
示例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")))
}
}
}
}
}
示例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)}
}
}
示例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
}
}
示例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"
}
}