TA的每日心情  | 开心 2021-3-12 23:18 | 
|---|
 
  签到天数: 2 天 [LV.1]初来乍到  
 | 
 
| 
 
 在实际中,常有需要“等待-直到某个条件满足”这样的场合,下面的例子可以用作模型,加以扩展。  
 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"); 
                        } |   
 
 
 
 |