Java의 동시성에 대한 Sun의 자습서에서 예를 재 작성하고 싶습니다. 원래 코드는 다음과 같습니다. http://java.sun.com/docs/books/tutorial/essential/concurrency/deadlock.html스칼라에서 '교착 상태'
이 코드는 올바르지 않습니다. 그것은 주석이 나타내는 곳에서 멈 춥니 다. 아무도 이것을 고칠 수 있습니까? 미리 감사드립니다.
import scala.actors.Actor
class Person(val name: String) extends Actor {
def bow(other: Person) {
other ! Bow(this)
}
private def bowBack(backTo: Person) {
println(this.name + " is bowing back to " + backTo.name)
backTo ! Bowed(this)
}
def act() {
while (true) {
receive {
case Bow(p) =>
println(this.name + " is bowing to " + p.name)
p ! BowBack(this)
println(" wating for bowing back...")
var received = false
while (true && received == false) {
receive { //blocked here
case Bowed(other) if p == other =>
println(" ... " + this.name + " has bowed to " + other.name)
received == true
}
}
println(this.name + " has bowed to " + p.name)
case BowBack(p) =>
println(this.name + " is bowing back to " + p.name)
p ! Bowed(this)
case "EXIT" => return
case x => println(x)
}
}
}
}
abstract case class Action()
case class Bow(person: Person) extends Action
case class BowBack(person: Person) extends Action
case class Bowed(person: Person) extends Action
object BowTest extends Application {
val a = new Person("Alphone")
val g = new Person("Gaston")
a.start()
g.start()
a ! Bow(g)
//g.bow(a)
a ! "EXIT"
g ! "EXIT"
}
교착 상태를 재현하는 데 필요한 최소 코드까지 줄일 수 있습니까? – Andrey