Skip to content

Instantly share code, notes, and snippets.

View vladimir-bukhtoyarov's full-sized avatar
🏠
Working from home

Vladimir Bukhtoyarov vladimir-bukhtoyarov

🏠
Working from home
  • VK
  • Saint-Petersburg, Russia
  • 20:26 (UTC +03:00)
View GitHub Profile
<dependency>
<groupId>org.gridkit.lab</groupId>
<artifactId>nanocloud</artifactId>
<version>0.8.8</version>
</dependency>
<dependency>
<groupId>org.gridkit.lab</groupId>
<artifactId>telecontrol-ssh</artifactId>
<version>0.8.8</version>
</dependency>
import java.lang.ref.WeakReference;
import java.util.concurrent.*;
import java.util.function.Consumer;
public class SchedulerLeakProtector {
public static <T> ScheduledFuture<?> scheduleAtFixedRate(ScheduledExecutorService scheduler, T target, Consumer<T> consumer, long initialDelay, long period, TimeUnit timeUnit) {
CompletableFuture<ScheduledFuture<?>> scheduledFutureFuture = new CompletableFuture<>();
LeakProtectedRunnable<T> runnable = new LeakProtectedRunnable<>(target, consumer, scheduledFutureFuture);
ScheduledFuture<?> scheduledFuture = scheduler.scheduleAtFixedRate(runnable, initialDelay, period, timeUnit);
/**
* Executor which useful for unit testing
*/
public class CurrentThreadExecutor implements ExecutorService {
@Override
public void execute(Runnable command) {
command.run();
}