java time wait_Java TimeUnit.timedWait方法代码示例

import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类

/**

* Waits for a Locator status request response to be returned up to the specified timeout in the

* given unit of time. This call will send status requests at fixed intervals in the given unit of

* time until the timeout expires. If the request to determine the Locator's status is successful,

* then the Locator is considered to be 'ONLINE'. Otherwise, the Locator is considered to be

* unresponsive to the status request.

*

* However, this does not necessarily imply the Locator start was unsuccessful, only that a

* response was not received in the given time period.

*

* Note, this method does not block or cause the Locator's location-based services (daemon

* Threads) to continue running in anyway if the main application Thread terminates when running

* the Locator in-process. If the caller wishes to start a Locator in an asynchronous manner

* within the application process, then a call should be made to waitOnLocator.

*

* @param timeout a long value in time unit indicating when the period of time should expire in

* attempting to determine the Locator's status.

* @param interval a long value in time unit for how frequent the requests should be sent to the

* Locator.

* @param timeUnit the unit of time in which the timeout and interval are measured.

* @return the state of the Locator, which will either be 'ONLINE' or "NOT RESPONDING'. If the

* status returned is 'NOT RESPONDING', it just means the Locator did not respond to the

* status request within the given time period. It should not be taken as the Locator

* failed to start.

* @see #waitOnLocator()

*/

public LocatorState waitOnStatusResponse(final long timeout, final long interval,

final TimeUnit timeUnit) {

final long endTimeInMilliseconds = (System.currentTimeMillis() + timeUnit.toMillis(timeout));

while (System.currentTimeMillis() < endTimeInMilliseconds) {

try {

LocatorStatusResponse response = statusLocator(getPort(), getBindAddress());

return new LocatorState(this, Status.ONLINE, response);

} catch (Exception ignore) {

try {

synchronized (this) {

timeUnit.timedWait(this, interval);

}

} catch (InterruptedException ignoreInterrupt) {

// NOTE just go and send another status request to the Locator...

}

}

}

// NOTE just because we were not able to communicate with the Locator in the given amount of

// time does not mean

// the Locator is having problems. The Locator could be slow in starting up and the timeout may

// not be

// long enough.

return new LocatorState(this, Status.NOT_RESPONDING);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值