Java Code Examples for java.util.concurrent.TimeUnit#name()

The following examples show how to use java.util.concurrent.TimeUnit#name() . 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: SenderFuture.java    From xian with Apache License 2.0 6 votes vote down vote up
@Override
public UnitResponse get(long timeout, TimeUnit unit) throws TimeoutException {
    if (timeout <= 0) {
        throw new IllegalArgumentException("超时时间必须为正数:" + timeout);
    }
    long timeoutInMilliseconds = unit.toMillis(timeout);
    synchronized (responseLock) {
        while (!isDone()) {
            try {
                responseLock.wait(timeoutInMilliseconds);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (!isDone()) {
                throw new TimeoutException("任务超时" + timeout + unit.name());
            }
        }
    }
    return unitResponse;
}
 
Example 2
Source File: SimpleTimeFormat.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private static String abbreviate(TimeUnit unit) {
	switch (unit) {
	case NANOSECONDS:
		return "ns";
	case MICROSECONDS:
		return "μs";
	case MILLISECONDS:
		return "ms";
	case SECONDS:
		return "s";
	case MINUTES:
		return "min";
	case HOURS:
		return "h";
	case DAYS:
		return "d";
	default:
		throw new RuntimeException("Unsupported unit " + unit.name());
	}
}
 
Example 3
Source File: ArchiveEventEncoderTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
void testEncodeSessionStateChange()
{
    final int offset = 24;
    final TimeUnit from = DAYS;
    final TimeUnit to = MILLISECONDS;
    final long sessionId = Long.MAX_VALUE;
    final String payload = from.name() + STATE_SEPARATOR + to.name();
    final int length = payload.length() + SIZE_OF_LONG + SIZE_OF_INT;
    final int captureLength = captureLength(length);

    final int encodedLength = encodeSessionStateChange(
        buffer, offset, captureLength, length, from, to, sessionId);

    assertEquals(encodedLength(sessionStateChangeLength(from, to)), encodedLength);
    assertEquals(captureLength, buffer.getInt(offset, LITTLE_ENDIAN));
    assertEquals(length, buffer.getInt(offset + SIZE_OF_INT, LITTLE_ENDIAN));
    assertNotEquals(0, buffer.getLong(offset + SIZE_OF_INT * 2, LITTLE_ENDIAN));
    assertEquals(sessionId, buffer.getLong(offset + LOG_HEADER_LENGTH));
    assertEquals(payload, buffer.getStringAscii(offset + LOG_HEADER_LENGTH + SIZE_OF_LONG));
}
 
Example 4
Source File: ClusterEventLoggerTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
void logStateChange()
{
    final int offset = ALIGNMENT * 11;
    logBuffer.putLong(CAPACITY + TAIL_POSITION_OFFSET, offset);
    final TimeUnit from = MINUTES;
    final TimeUnit to = SECONDS;
    final int memberId = 42;
    final String payload = from.name() + STATE_SEPARATOR + to.name();
    final int captureLength = SIZE_OF_INT * 2 + payload.length();

    logger.logStateChange(STATE_CHANGE, from, to, memberId);

    verifyLogHeader(logBuffer, offset, toEventCodeId(STATE_CHANGE), captureLength, captureLength);
    assertEquals(memberId, logBuffer.getInt(encodedMsgOffset(offset + LOG_HEADER_LENGTH), LITTLE_ENDIAN));
    assertEquals(payload, logBuffer.getStringAscii(encodedMsgOffset(offset + LOG_HEADER_LENGTH + SIZE_OF_INT)));
}
 
Example 5
Source File: RateLimiter.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Set the RateLimiter max available resources and refill period.
 * @param limit The max value available resource units can be refilled to.
 * @param timeUnit Timeunit factor for translating to ms.
 */
public synchronized void set(final long limit, final TimeUnit timeUnit) {
  switch (timeUnit) {
  case MILLISECONDS:
    tunit = 1;
    break;
  case SECONDS:
    tunit = 1000;
    break;
  case MINUTES:
    tunit = 60 * 1000;
    break;
  case HOURS:
    tunit = 60 * 60 * 1000;
    break;
  case DAYS:
    tunit = 24 * 60 * 60 * 1000;
    break;
  default:
    throw new RuntimeException("Unsupported " + timeUnit.name() + " TimeUnit.");
  }
  this.limit = limit;
  this.avail = limit;
}
 
Example 6
Source File: JcloudsUtil.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
public static String waitForPasswordOnAws(ComputeService computeService, final NodeMetadata node, long timeout, TimeUnit timeUnit) throws TimeoutException {
    ComputeServiceContext computeServiceContext = computeService.getContext();
    AWSEC2Api ec2Client = computeServiceContext.unwrapApi(AWSEC2Api.class);
    final WindowsApi client = ec2Client.getWindowsApi().get();
    final String region = node.getLocation().getParent().getId();

    // The Administrator password will take some time before it is ready - Amazon says sometimes 15 minutes.
    // So we create a predicate that tests if the password is ready, and wrap it in a retryable predicate.
    Predicate<String> passwordReady = new Predicate<String>() {
        @Override public boolean apply(String s) {
            if (Strings.isNullOrEmpty(s)) return false;
            PasswordData data = client.getPasswordDataInRegion(region, s);
            if (data == null) return false;
            return !Strings.isNullOrEmpty(data.getPasswordData());
        }
    };

    LOG.info("Waiting for password, for "+node.getProviderId()+":"+node.getId());
    Predicate<String> passwordReadyRetryable = Predicates2.retry(passwordReady, timeUnit.toMillis(timeout), 10*1000, TimeUnit.MILLISECONDS);
    boolean ready = passwordReadyRetryable.apply(node.getProviderId());
    if (!ready) throw new TimeoutException("Password not available for "+node+" in region "+region+" after "+timeout+" "+timeUnit.name());

    // Now pull together Amazon's encrypted password blob, and the private key that jclouds generated
    PasswordDataAndPrivateKey dataAndKey = new PasswordDataAndPrivateKey(
            client.getPasswordDataInRegion(region, node.getProviderId()),
            node.getCredentials().getPrivateKey());

    // And apply it to the decryption function
    WindowsLoginCredentialsFromEncryptedData f = computeServiceContext.utils().injector().getInstance(WindowsLoginCredentialsFromEncryptedData.class);
    LoginCredentials credentials = f.apply(dataAndKey);

    return credentials.getPassword();
}
 
Example 7
Source File: FluxTakeTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
public void await(int timeout, TimeUnit unit) throws InterruptedException {
	if (!countDownLatch.await(timeout, unit)) {
		throw new RuntimeException("Expected Completion within "+ timeout +
				" " + unit.name() + " but Complete signal was not emitted");
	}
}
 
Example 8
Source File: Moment.java    From Time4A with Apache License 2.0 4 votes vote down vote up
@Override
public Moment withValue(
    Moment context,
    TimeUnit value,
    boolean lenient
) {

    if (value == null) {
        throw new IllegalArgumentException("Missing precision.");
    }

    Moment result;

    switch (value) {
        case DAYS:
            long secsD = MathUtils.floorDivide(context.posixTime, 86400) * 86400;
            return Moment.of(secsD, TimeScale.POSIX);
        case HOURS:
            long secsH = MathUtils.floorDivide(context.posixTime, 3600) * 3600;
            return Moment.of(secsH, TimeScale.POSIX);
        case MINUTES:
            long secsM = MathUtils.floorDivide(context.posixTime, 60) * 60;
            return Moment.of(secsM, TimeScale.POSIX);
        case SECONDS:
            result = Moment.of(context.posixTime, 0, TimeScale.POSIX);
            break;
        case MILLISECONDS:
            int f3 = (context.getNanosecond() / MIO) * MIO;
            result = Moment.of(context.posixTime, f3, TimeScale.POSIX);
            break;
        case MICROSECONDS:
            int f6 = (context.getNanosecond() / 1000) * 1000;
            result = Moment.of(context.posixTime, f6, TimeScale.POSIX);
            break;
        case NANOSECONDS:
            return context;
        default:
            throw new UnsupportedOperationException(value.name());
    }

    if (context.isLeapSecond() && LeapSeconds.getInstance().isEnabled()) {
        return result.plus(1, SI.SECONDS);
    } else {
        return result;
    }

}
 
Example 9
Source File: PropUtil.java    From tddl5 with Apache License 2.0 4 votes vote down vote up
/**
 * Formats a String duration property (time + optional unit). value in millis.
 */
public static String formatDuration(long time, TimeUnit unit) {
    return String.valueOf(time) + ' ' + unit.name();
}
 
Example 10
Source File: TimeUtils.java    From flink with Apache License 2.0 4 votes vote down vote up
private static String createTimeUnitString(TimeUnit timeUnit) {
	return timeUnit.name() + ": (" + String.join(" | ", timeUnit.getLabels()) + ")";
}
 
Example 11
Source File: TimeWithUnit.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
public TimeWithUnit(TimeUnit unit, long time) {
  this.timeUnit = unit.name();
  this.time = time;
}