- java.lang.Object
-
- jdk.management.resource.SimpleMeter
-
- jdk.management.resource.NotifyingMeter
-
- jdk.management.resource.ThrottledMeter
-
- All Implemented Interfaces:
ResourceMeter
,ResourceRequest
public class ThrottledMeter extends NotifyingMeter
A ThrottledMeter with bandwidth limit, an approver callback for requests and allocates from an optional parent. The rate is limited by the throttle limit and is settable at any time. The rate-per-second is the amount that can be approved each second. An amount not used in each second is accumulated up to a maximum of one seconds.- Since:
- 8u40
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ThrottledMeter
create(ResourceType type, long ratePerSec, ResourceApprover approver)
Create a new meter with a throttle rate and approver.static ThrottledMeter
create(ResourceType type, long ratePerSec, ResourceRequest parent, ResourceApprover approver)
Create a new meter with a throttle rate, parent and approver.static ThrottledMeter
create(ResourceType type, ResourceRequest parent, ResourceApprover approver)
Create a new meter with unlimited rate, parent and approver.long
getCurrentRate()
Returns an estimate of the current rate in amount per second.long
getRatePerSec()
Returns the current throttle rate per second.long
setRatePerSec(long ratePerSec)
Sets the throttle rate per second.String
toString()
Returns a string representation of the meter.long
validate(long previous, long amount, ResourceId id)
Validates the amount.-
Methods inherited from class jdk.management.resource.NotifyingMeter
create, getApprover, getGranularity, setGranularity
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from class jdk.management.resource.SimpleMeter
create, create, equals, getAllocated, getParent, getType, getValue, hashCode, request
-
-
-
-
Method Detail
-
create
public static ThrottledMeter create(ResourceType type, long ratePerSec, ResourceApprover approver)
Create a new meter with a throttle rate and approver.- Parameters:
type
- the ResourceTyperatePerSec
- the rate allowed for Throttling; bytes-per sec; must be greater than zeroapprover
- the ResourceApprover to be notified; may benull
- Returns:
- a new ThrottledMeter
- Throws:
IllegalArgumentException
- if ratePerSec is less than or equal to zero
-
create
public static ThrottledMeter create(ResourceType type, ResourceRequest parent, ResourceApprover approver)
Create a new meter with unlimited rate, parent and approver.- Parameters:
type
- the ResourceTypeparent
- a ResourceRequest to request from; may benull
approver
- the ResourceApprover to be notified; may benull
- Returns:
- a new ThrottledMeter
- Throws:
IllegalArgumentException
- if ratePerSec is less than or equal to zero
-
create
public static ThrottledMeter create(ResourceType type, long ratePerSec, ResourceRequest parent, ResourceApprover approver)
Create a new meter with a throttle rate, parent and approver.- Parameters:
type
- the ResourceTyperatePerSec
- the rate allowed for throttling; bytes-per sec; must be greater than zeroparent
- a ResourceRequest to request from; may benull
approver
- the ResourceApprover to be notified; may benull
- Returns:
- a new ThrottledMeter
- Throws:
IllegalArgumentException
- if ratePerSec is less than or equal to zero
-
validate
public long validate(long previous, long amount, ResourceId id)
Validates the amount. If returning an amount, just return it. Throttling is achieved by an algorithm similar to the token bucket. Anallowance
is maintained of the amount than can be immediately fulfilled to satisfy a request. The allowance maximum is two seconds worth at the current rate-per-second. Requests reduce the allowance by the amount of the request. If the allowance is initially positive, the request is fulfilled immediately. If the allowance is negative, the allowance is topped up based on the elapsed time, waiting until it is positive again. If multiple threads are contending for the allowance, normal thread scheduling and synchronization primitives influence the order.If an InterruptedException is caught, zero is returned, failing the validation.
The throttling of resource use is decoupled from the underlying resource implementation and only delays the start of resource consumption not the rate of consumption.
- Overrides:
validate
in classNotifyingMeter
- Parameters:
amount
- the amount requestedid
- the ResourceId for the resource instance; may benull
previous
- the previous meter value- Returns:
- the value approved
-
getCurrentRate
public final long getCurrentRate()
Returns an estimate of the current rate in amount per second.- Returns:
- an estimate of the current rate in amount per second
-
getRatePerSec
public final long getRatePerSec()
Returns the current throttle rate per second.- Returns:
- the current throttle rate per second
-
setRatePerSec
public final long setRatePerSec(long ratePerSec)
Sets the throttle rate per second.- Parameters:
ratePerSec
- the rate per second; must be greater than zero- Returns:
- the previous throttle rate per second
- Throws:
IllegalArgumentException
- if ratePerSec is less than or equal to zero
-
toString
public String toString()
Description copied from class:SimpleMeter
Returns a string representation of the meter.- Overrides:
toString
in classSimpleMeter
- Returns:
- a string representation of the meter
-
-