2009-12-10 1 views
2

그래서 실제로 스레드를 차단하지 않고 이 나타나는 것입니다.이 차단되고있는 네트워크 코드를 작성하고 싶습니다. 전선에서 일부 데이터를 보내고 네트워크를 통해 되돌아 오는 '큐'응답을 보냅니다. 나는 여기에있는 배우 튜토리얼에서 생산자/소비자 예제에서 영감 개념의 매우 간단한 증거를 쓴 : http://www.scala-lang.org/node/242 것은이 스레드를 취할 나타납니다받을 사용하고, 그래서 내가 궁금하네요액터 'threadless'(no receive ...)를 사용하여 Scala에서 제작자/소비자 obj를 만들 수 있습니까?

어쨌든 스레드를 사용하지 않고 여전히 '블로킹 감각'을 얻으려면. Heres 내 코드 샘플 :

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 => receive { case r:Request => 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()); 
} 

수신을 사용하지 않고 이렇게 스레드가없는 방법이 있습니까?

답변

3

나는 수신보다는 반응을 사용하고 배우가 수신처럼 스레드를 차지하지 않을 것으로 기대합니다. 이 문제에 관한 스레드는 receive vs react입니다.

4

직접 릴리스를 스레드 차단해야하는 react에 의존 수 :

class Connection { 
    private val act:Actor = actor { 
    loop { 
     react { 
     case r:Request => reply { r } 
     } 
    } 
    } 
[...] 
관련 문제