Basic Scala Actors: Examples from Books Don't "Act" at All

I am very new to Scala and too rusty in Java to consider myself anything other than a complete newbie. So I take easy steps to learn it.

When I looked at the actors, I tried a few things, but came across numerous NoClassDefFound errors. Ultimately, I decided to take an example of a book and build on top of it, instead of debugging my first attempt. Surprise: book examples do not work properly!

Here is an example from O'Reilly Scala programming:

import scala.actors.Actor class Redford extends Actor { def act() { println("A lot of what acting is, is paying attention.") } } val robert = new Redford robert.start 

He suggested, when it was executed, to print a quote from Redford. However, when I run it, nothing happens, I return to the command line:

 D:\prog\scala-2.8.1.final\pierric>scala testactors.scala D:\prog\scala-2.8.1.final\pierric> 

Another example is seven programming languages ​​in seven weeks. This is so (I just changed the lines from laziness):

 import scala.actors._ import scala.actors.Actor._ case object Poke; case object Feed; class Kid() extends Actor { def act() { loop { react { case Poke => { println("Ow") println("Quit it") } case Feed => { println("gurgle") println("burp") } } } } } var bart = new Kid().start var lisa = new Kid().start println("starting") bart ! Poke lisa ! Poke bart ! Feed lisa ! Feed 

This time, he should return a randomly ordered sequence of "ow quit it" and "gurgle burp". However, when I run it:

 D:\prog\scala-2.8.1.final\pierric>scala testkids.scala starting D:\prog\scala-2.8.1.final\pierric> 

Now one more funny thing. If I add a simple println line at the beginning of my action, follow these steps:

 class Kid() extends Actor { def act() { println("Kid initializing") loop { react { ... 

Then I get most of the time:

 D:\prog\scala-2.8.1.final\pierric>scala testkids.scala starting Kid initializing Kid initializing D:\prog\scala-2.8.1.final\pierric> 

But sometimes also:

 starting Kid initializing Kid initializing scala.actors.Actor$$anon$1@5a9de6 : caught java.lang.NoClassDefFoundError: Main$$anon$1$Fee java.lang.NoClassDefFoundError: Main$$anon$1$Feed$ at Main$$anon$1.Main$$anon$$Feed(testkids.scala:5) at Main$$anon$1$$anonfun$1.apply$mcV$sp(testkids.scala:31) at scala.actors.Actor$$anon$1.act(Actor.scala:135) at scala.actors.Reactor$$anonfun$dostart$1.apply(Reactor.scala:222) at scala.actors.Reactor$$anonfun$dostart$1.apply(Reactor.scala:222) at scala.actors.ReactorTask.run(ReactorTask.scala:36) at scala.concurrent.forkjoin.ForkJoinPool$AdaptedRunnable.exec(ForkJoinPool.java:6 at scala.concurrent.forkjoin.ForkJoinTask.quietlyExec(ForkJoinTask.java:422) at scala.concurrent.forkjoin.ForkJoinWorkerThread.mainLoop(ForkJoinWorkerThread.ja at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:32 Caused by: java.lang.ClassNotFoundException: Main$$anon$1$Feed$ at java.net.URLClassLoader$1.run(Unknown Source) 

So here I am, ignorant ... as examples "from a book", actually from two books! and don't seem to work. I tried on two different machines, which apparently have different JVMs. In both cases, I ran scala 2.8.1.final. On one machine 32-bit Windows XP runs, the other on 64-bit Windows 7. I did not find anything related to this problem with googling ...

Thanks in advance to everyone who can shed light on this!

Pierric.

+4
source share
1 answer

This is because the scala script runs as quickly as possible once they are completed in the main thread. This works very poorly in settings with multiple threads (see Can actors in scala not process messages? (Example in O'Reilly's Programming Scala) ). If instead you run scala and load the scripts as follows:

 # scala scala> :load testactors.scala Loading testactors.scala... import scala.actors.Actor defined class Redford robert: Redford = Redford@29e07d3e res0: scala.actors.Actor = Redford@29e07d3e scala> A lot of what acting is, is paying attention. 
+8
source

Source: https://habr.com/ru/post/1347862/


All Articles