Java Code Examples for java.util.concurrent.locks.LockSupport#parkUntil()

The following examples show how to use java.util.concurrent.locks.LockSupport#parkUntil() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RpcLocker.java    From sumk with Apache License 2.0 6 votes vote down vote up
public RpcResult awaitForResponse() {
	RpcResult rpcResult = this.result.get();
	if (rpcResult != null) {
		return rpcResult;
	}
	Thread currentThread = Thread.currentThread();
	if (!awaitThread.compareAndSet(null, currentThread)) {
		throw new SoaException(RpcErrorCode.TIMEOUT, "cannot await twice", new TimeoutException());
	}
	while (result.get() == null) {

		LockSupport.parkUntil(System.currentTimeMillis() + 10000);
	}

	rpcResult = this.result.get();
	if (rpcResult == null) {
		rpcResult = RpcResult.timeout(req);
	}
	return rpcResult;
}
 
Example 2
Source File: PipeImpl.java    From baratine with GNU General Public License v2.0 6 votes vote down vote up
public void block(long blockSequence)
{
  long expire = System.currentTimeMillis() + _timeout;
  
  Thread thread = Thread.currentThread();
  _thread = thread;
  
  try {
    while (_readySequence < blockSequence
           && (System.currentTimeMillis() < expire)) {
      LockSupport.parkUntil(expire);
    }
  } catch (Exception e) {
    log.log(Level.FINER, e.toString(), e);
  } finally {
    _thread = null;
  }
}
 
Example 3
Source File: BaseConsumerTask.java    From hermes with Apache License 2.0 5 votes vote down vote up
protected void waitForNextTryTime(long nextTryTime) {
	while (true) {
		if (!isClosed() && !Thread.currentThread().isInterrupted()) {
			if (nextTryTime > m_systemClockService.now()) {
				LockSupport.parkUntil(nextTryTime);
			} else {
				break;
			}
		} else {
			return;
		}
	}
}
 
Example 4
Source File: RingBlockerSinglePoller.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final boolean pollWait(long sequence,
                              long timeout,
                              TimeUnit unit)
{
  if (! _queue.isEmpty()) {
    return true;
  }
  
  if (timeout <= 0) {
    return false;
  }
  

  Thread thread = Thread.currentThread();
  
  Thread oldThread = _pollThreadRef.getAndSet(thread);
  
  if (oldThread != null) {
    LockSupport.unpark(oldThread);
  }
  
  long expires = CurrentTime.getCurrentTimeActual() + unit.toMillis(timeout);
  
  while (_queue.isEmpty() && CurrentTime.getCurrentTimeActual() < expires) {
    try {
      Thread.interrupted();
      
      LockSupport.parkUntil(expires);
    } catch (Exception e) {
      
    }
  }
  
  _pollThreadRef.compareAndSet(thread, null);
  
  return ! _queue.isEmpty();
}
 
Example 5
Source File: ResultJampRpc.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
public T get(long time, TimeUnit unit)
{
  if (! _isDone) {
    long timeout = unit.toMillis(time);
    long expires = timeout + CurrentTime.getCurrentTimeActual();
    
    Thread thread = Thread.currentThread();
    
    _thread = thread;
    
    while (! _isDone && CurrentTime.getCurrentTimeActual() < expires) {
      try {
        Thread.interrupted();
        LockSupport.parkUntil(expires);
      } catch (Exception e) {
      }
    }
    
    _thread = null;
    
    if (! _isDone) {
      throw new ServiceExceptionTimeout(L.l("jamp-rpc timeout"));
    }
  }
  
  if (_exception != null) {
    throw ServiceException.createAndRethrow(_exception);
  }
  else {
    return _value;
  }
}
 
Example 6
Source File: BasicFuture.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
@Override
public T get(long timeout, TimeUnit unit)
    throws InterruptedException,
           ExecutionException, 
           TimeoutException
{
  long expires = System.currentTimeMillis() + unit.toMillis(timeout);

  Thread thread = Thread.currentThread();
  
  try {
    _thread = thread;
    
    while (! _isDone && System.currentTimeMillis() < expires) {
      LockSupport.parkUntil(expires);
    }
  } finally {
    _thread = null;
  }
  
  if (! _isDone) {
    throw new TimeoutException("timeout: " + timeout + " " + unit);
  }
  
  if (_exn != null) {
    throw _exn;
  }
  else {
    return _value;
  }
}
 
Example 7
Source File: ForkJoinPool.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Possibly triggers shutdown and tries (once) to block worker
 * when pool is (or may be) quiescent. Waits up to a duration
 * determined by number of workers.  On timeout, if ctl has not
 * changed, terminates the worker, which will in turn wake up
 * another worker to possibly repeat this process.
 *
 * @param w the calling worker
 * @return negative if w should terminate
 */
private int timedAwaitWork(WorkQueue w, long c) {
    int stat = 0;
    int scale = 1 - (short)(c >>> TC_SHIFT);
    long deadline = (((scale <= 0) ? 1 : scale) * IDLE_TIMEOUT_MS +
                     System.currentTimeMillis());
    if ((runState >= 0 || (stat = tryTerminate(false, false)) > 0) &&
        w != null && w.scanState < 0) {
        int ss; AuxState aux;
        w.parker = Thread.currentThread();
        if (w.scanState < 0)
            LockSupport.parkUntil(this, deadline);
        w.parker = null;
        if ((runState & STOP) != 0)
            stat = w.qlock = -1;         // pool terminating
        else if ((ss = w.scanState) < 0 && !Thread.interrupted() &&
                 (int)c == ss && (aux = auxState) != null && ctl == c &&
                 deadline - System.currentTimeMillis() <= TIMEOUT_SLOP_MS) {
            aux.lock();
            try {                        // pre-deregister
                WorkQueue[] ws;
                int cfg = w.config, idx = cfg & SMASK;
                long nc = ((UC_MASK & (c - TC_UNIT)) |
                           (SP_MASK & w.stackPred));
                if ((runState & STOP) == 0 &&
                    (ws = workQueues) != null &&
                    idx < ws.length && idx >= 0 && ws[idx] == w &&
                    U.compareAndSwapLong(this, CTL, c, nc)) {
                    ws[idx] = null;
                    w.config = cfg | UNREGISTERED;
                    stat = w.qlock = -1;
                }
            } finally {
                aux.unlock();
            }
        }
    }
    return stat;
}
 
Example 8
Source File: LockSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void park(long millis) {
    LockSupport.parkUntil(deadline(millis));
}
 
Example 9
Source File: LockSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
void park(long millis) {
    LockSupport.parkUntil(theBlocker(), deadline(millis));
}
 
Example 10
Source File: ResultFuture.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
public T get(long timeout, TimeUnit unit)
{
  if (isDone()) {
    return getResultValue();
  }
  
  Thread thread = Thread.currentThread();
  
  //_thread = thread;
    
  long expires = unit.toMillis(timeout) + System.currentTimeMillis();
  
  while (true) {
    if (isDone()) {
      return getResultValue();
    }
    else if (_state == FutureState.ASYNC) {
      Result<Object> chain = _chain;
      Object chainValue = _chainValue;
      _chain = null;
      _chainValue = null;
      
      _state = FutureState.INIT;
        
      //_thread = null;
      
      chain.completeFuture(chainValue);
      
      /*
      if (isDone()) {
        return getResultValue();
      }
      */
      
      // _thread = thread;
    }
    else {
      if (ServiceRef.flushOutboxAndExecuteLast()) {
        // if pending messages, continue to process them
        continue;
      }
      
      // ServiceRef.flushOutbox();
      
      _thread = thread;
    
      if (_state.isParkRequired()) {
        if (expires < System.currentTimeMillis()) {
          _thread = null;
          
          throw new ServiceExceptionFutureTimeout("future timeout " + timeout + " " + unit);
        }
        
        LockSupport.parkUntil(expires);
      }
      
      _thread = null;
    }
  }
}
 
Example 11
Source File: ProfileTask.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void run()
{
  try {
    long period = _period;

    long startRun = System.currentTimeMillis();
    long startSample = startRun;

    _ticks = 0;
    
    _gcStartTime = 0;
    _gcEndTime = 0;
    
    if (_memoryBean != null) {
      _gcStartTime = _memoryBean.getGarbageCollectionTime();
    }

    while (_profileThread.get() == this) {
      long expires = startSample + _period;

      synchronized (ProfileTask.class) {
        _ticks++;

        nativeProfile(_jniProfile, _maxDepth);
      }

      long endSample = System.currentTimeMillis();

      if (endSample < expires) {
        expires = expires - expires % period;

        while (endSample < expires) {
          Thread.interrupted();
          LockSupport.parkUntil(expires);

          endSample = System.currentTimeMillis();
        }
      }

      startSample = endSample;
    }

    long time = System.currentTimeMillis() - startRun;
    long expectedTicks = time / period;
    
    if (_memoryBean != null)
      _gcEndTime = _memoryBean.getGarbageCollectionTime();
    
    log.info(this + " complete: " + time / 1000 + "s, "
             + " period=" + period + "ms,"
             + " gc-time=" + (_gcEndTime - _gcStartTime) + "ms,"
             + " missed ticks=" + (expectedTicks - _ticks)
             + String.format(" (%.2f%%),",
                             100.0 * (expectedTicks - _ticks) / _ticks)
                             + " total ticks=" + _ticks);
  } catch (Exception e) {
    log.log(Level.WARNING, e.toString(), e);
  } finally {
    _profileThread.compareAndSet(this, null);
  }
}
 
Example 12
Source File: LockSupportTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
void park(long millis) {
    LockSupport.parkUntil(deadline(millis));
}
 
Example 13
Source File: LockSupportTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
void park(long millis) {
    LockSupport.parkUntil(theBlocker(), deadline(millis));
}
 
Example 14
Source File: OldDeletedEntriesCleanupThread.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
private void sleepMillis(long millis) {
    long deadline = System.currentTimeMillis() + millis;
    while (System.currentTimeMillis() < deadline && !shutdown)
        LockSupport.parkUntil(cleanupSleepingHandle, deadline);
}