2016-06-04 2 views
0

나는 다음과 같은 테스트 클래스가 있습니다java.lang.IllegalMonitorStateException 테스트 할 때 스칼라 미래

test("Any should return the first future") { 
    val p = Promise[Int]() 
    p completeWith Future.any(List(Future{wait(2000); 1}, Future{wait(1); 2})) 
    whenReady(p.future) {x => 
     assert(true) 
    } 
    } 

(I : 그것은 내

@RunWith(classOf[JUnitRunner]) 
class NodeScalaSuite extends FunSuite with ScalaFutures { 

, 나는 미래를 반환하는 방법을 확인하려면이 테스트를 추가 더 간단하게 디버깅하기 위해 assert를 true으로 만들었습니다.)

테스트 스위트를 실행할 때 다음 오류가 발생합니다.

[info] The future returned an exception of type: java.lang.IllegalMonitorStateException. 

어떻게 될 수 있습니까?

답변

1

java.lang.Object#wait 용 문서에 따르면 이는

throws IllegalMonitorStateException if the current thread is not the owner of the object's monitor.

waitsynchronized이 블록 내에서 호출되어야 의미한다. synchronized { wait(2000) }과 같은 것이 작동해야하지만 실제로 원하는 것은 Thread.sleep(2000)을 사용하는 것입니다. wait은 복수 스레드의 공유 리소스에 대한 액세스를 동기화하기 위해 notifynotifyAll과 함께 사용하기위한 것입니다. 다른 스레드가 동일한 동기화 된 블록을 실행할 수 있도록 객체의 모니터를 해제합니다.