当前页面: 开发资料首页 → Java 专题 → 等待-直到模式
等待-直到模式
摘要: 等待-直到模式
</td>
</tr>
<tr>
<td width="525" height="35" valign="top" class="ArticleTeitle"> 在实际中,常有需要“等待-直到某个条件满足”这样的场合,下面的例子可以用作模型,加以扩展。
public class FullWait extends Object {
private volatile int value;
public FullWait(int initialValue) {
value = initialValue;
}
public synchronized void setValue(int newValue) {//改变value
if ( value != newValue ) {
value = newValue;
notifyAll();//通知等待的线程,value的值已发生变化
}
}
public synchronized boolean waitUntilAtLeast(
int minValue,
long msTimeout //超时值
) throws InterruptedException {
if ( msTimeout == 0L ) {
while ( value < minValue ) {
wait(); // 等待直到满足value>=minvalue
}
// condition has finally been met
return true;
}
// only wait for the specified amount of time
long endTime = System.currentTimeMillis() + msTimeout;
long msRemaining = msTimeout;
while ( ( value < minValue ) && ( msRemaining > 0L ) ) {
wait(msRemaining);
msRemaining = endTime - System.currentTimeMillis();
}//一直等待到达超时,或者满足最小值要求。
// May have timed out, or may have met value,
// calc return value.
return ( value >= minValue );
}
public String toString() {
return getClass().getName() + "[value=" + value + "]";
}
}
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
<td width="181" align="center" valign="top" class="ArticleTeitle">
</td>
</tr>
<tr>
<td height="25" colspan="2" valign="top" class="ArticleTeitle">
↑返回目录
前一篇: 先进先出的队列
后一篇: 如何创建自运行对象