void block()
阻塞當前線程,直到條件爲open
void block(long timeout)
阻塞當前線程,直到條件爲open或超時
void open()
釋放所有阻塞的線程
void close()
將條件重置爲close
一個簡單的例子
void testConditionVariable(){
final ConditionVariable startDone = new ConditionVariable();
new Thread() {
@Override
public void run() {
try {
//比如這裏有一個耗時間的操作,如open camera
} catch (RuntimeException e) {
}
startDone.open();
}
}.start();
if (!startDone.block(5000)) {
//主線程在此阻塞,等待startDone.open(),
//如果5000ms後依舊沒有執行startDone.open()
//則,startDone.block返回false;
//執行了startDone.open()後,startDone.block返回true;
}
}
更多的 可以參考android官網
http://developer.android.com/reference/android/os/ConditionVariable.html