2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tesla.internal.throttler;
15 import java.util.HashMap;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.TimeUnit;
23 * The {@link ScheduledChannelThrottler} implements a throttler that maintains a
24 * single execution rates, and does not maintain order of calls (thus has to
25 * start from back rather than try to insert things in middle)
27 * @author Karel Goderis - Initial contribution
29 public final class ScheduledChannelThrottler extends AbstractChannelThrottler {
31 public ScheduledChannelThrottler(Rate totalRate) {
32 this(totalRate, Executors.newSingleThreadScheduledExecutor(), new HashMap<>(), TimeProvider.SYSTEM_PROVIDER);
35 public ScheduledChannelThrottler(Rate totalRate, Map<Object, Rate> channels) {
36 this(totalRate, Executors.newSingleThreadScheduledExecutor(), channels, TimeProvider.SYSTEM_PROVIDER);
39 public ScheduledChannelThrottler(Rate totalRate, ScheduledExecutorService scheduler, Map<Object, Rate> channels,
40 TimeProvider timeProvider) {
41 super(totalRate, scheduler, channels, timeProvider);
44 public void submitSync(Object channelKey, Runnable task) throws InterruptedException {
45 Thread.sleep(getThrottleDelay(channelKey));
49 public void submitSync(Runnable task) throws InterruptedException {
50 long delay = callTime(null) - timeProvider.getCurrentTimeInMillis();
51 Thread.sleep(getThrottleDelay(delay));
56 public Future<?> submit(Runnable task) {
57 long delay = callTime(null) - timeProvider.getCurrentTimeInMillis();
58 return scheduler.schedule(task, delay < 0 ? 0 : delay, TimeUnit.MILLISECONDS);
62 public Future<?> submit(Object channelKey, Runnable task) {
63 return scheduler.schedule(task, getThrottleDelay(channelKey), TimeUnit.MILLISECONDS);