Repository: netty/netty
File: common/src/main/java/io/netty/util/concurrent/AutoScalingEventExecutorChooserFactory.java (Java)

Code before:
 */
public final class AutoScalingEventExecutorChooserFactory implements EventExecutorChooserFactory {

    /**
     * A container for the utilization metric of a single EventExecutor.
     * This object is intended to be created once and have its {@code utilization}
     * field updated periodically.
     */
    public static final class AutoScalingUtilizationMetric {
        final EventExecutor executor;
        private volatile double utilization;

        AutoScalingUtilizationMetric(EventExecutor executor) {
            this.executor = executor;
        }

        /**
         * Returns the most recently calculated utilization for the associated executor.
         * @return a value from 0.0 to 1.0.
         */
        public double utilization() {
            return utilization;
        }

        void setUtilization(double utilization) {
            this.utilization = utilization;
        }
    }

    private static final Runnable NO_OOP_TASK = () -> { };
    private final int minChildren;
    private final int maxChildren;
    private final long utilizationCheckPeriodNanos;
    private final double scaleDownThreshold;
    private final double scaleUpThreshold;
    private final int maxRampUpStep;
    private final int maxRampDownStep;
    private final int scalingPatienceCycles;

    /**
     * Creates a new factory for a scaling-enabled {@link EventExecutorChooser}.
     *
     * @param minThreads               the minimum number of threads to keep active.
     * @param maxThreads               the maximum number of threads to scale up to.
     * @param utilizationWindow        the period at which to check group utilization.
     * @param windowUnit               the unit for {@code utilizationWindow}.
     * @param scaleDownThreshold       the average utilization below which a thread may be suspended.
     * @param scaleUpThreshold         the average utilization above which a thread may be resumed.
     * @param maxRampUpStep            the maximum number of threads to add in one cycle.
     * @param maxRampDownStep          the maximum number of threads to remove in one cycle.
     * @param scalingPatienceCycles    the number of consecutive cycles a condition must be met before scaling.

Review comment (suggestion):
nit: I think you could use lazySet for this one as it will only ever be modified from the same thread. 

Code after:
 * before a scaling action is triggered.
 */
public final class AutoScalingEventExecutorChooserFactory implements EventExecutorChooserFactory {

    /**
     * A container for the utilization metric of a single EventExecutor.
     * This object is intended to be created once and have its {@code utilization}
     * field updated periodically.
     */
    public static final class AutoScalingUtilizationMetric {
        private final EventExecutor executor;
        private final AtomicLong utilizationBits = new AtomicLong();

        AutoScalingUtilizationMetric(EventExecutor executor) {
            this.executor = executor;
        }

        /**
         * Returns the most recently calculated utilization for the associated executor.
         * @return a value from 0.0 to 1.0.
         */
        public double utilization() {
            return Double.longBitsToDouble(utilizationBits.get());
        }

        /**
         * Returns the {@link EventExecutor} this metric belongs too.
         * @return the executor.
         */
        public EventExecutor executor() {
            return executor;
        }

        void setUtilization(double utilization) {
            long bits = Double.doubleToRawLongBits(utilization);
            utilizationBits.lazySet(bits);
        }
    }

    private static final Runnable NO_OOP_TASK = () -> { };
    private final int minChildren;
    private final int maxChildren;
    private final long utilizationCheckPeriodNanos;
    private final double scaleDownThreshold;
    private final double scaleUpThreshold;
    private final int maxRampUpStep;
    private final int maxRampDownStep;
    private final int scalingPatienceCycles;

    /**
     * Creates a new factory for a scaling-enabled {@link EventExecutorChooser}.