FullWait.java
资源名称:Source.rar [点击查看]
上传用户:songled
上传日期:2022-07-14
资源大小:94k
文件大小:1k
源码类别:
进程与线程
开发平台:
Java
- public class FullWait extends Object {
- private volatile int value;
- public FullWait(int initialValue) {
- value = initialValue;
- }
- public synchronized void setValue(int newValue) {
- if ( value != newValue ) {
- value = newValue;
- notifyAll();
- }
- }
- public synchronized boolean waitUntilAtLeast(
- int minValue,
- long msTimeout
- ) throws InterruptedException {
- if ( msTimeout == 0L ) {
- while ( value < minValue ) {
- wait(); // wait indefinitely until notified
- }
- // 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 + "]";
- }
- }