Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible to make a producer/consumer obj in Scala using actors 'threadless' (no receive...)?
    text
    copied!<p>So I want to write some network code that <em>appears</em> to be blocking, without actually blocking a thread. I'm going to send some data out on the wire, and have a 'queue' of responses that will come back over the network. I wrote up a <em>very</em> simple proof of concept, inspired by the producer/consumer example on the actor tutorial found here: <a href="http://www.scala-lang.org/node/242" rel="nofollow noreferrer">http://www.scala-lang.org/node/242</a></p> <p>The thing is, using receive appears to take up a thread, and so I'm wondering if theres anyway to not take up a thread and still get the 'blocking feel'. Heres my code sample:</p> <pre><code>import scala.actors.Actor._; import scala.actors.Actor; case class Request(val s:String); case class Message(val s:String); class Connection { private val act:Actor = actor { loop { react { case m:Message =&gt; receive { case r:Request =&gt; reply { m } } } } } def getNextResponse(): Message = { return (act !? new Request("get")).asInstanceOf[Message]; } //this would call the network layer and send something over the wire def doSomething() { generateResponse(); } //this is simulating the network layer getting some data back //and sending it to the appropriate Connection object private def generateResponse() { act ! new Message("someData"); act ! new Message("moreData"); act ! new Message("even more data"); } } object runner extends Application { val conn = new Connection(); conn.doSomething(); println( conn.getNextResponse()); println(conn.getNextResponse()); println(conn.getNextResponse()); } </code></pre> <p>Is there a way to do this without using the receive, and thereby making it threadless?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload