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
  • 05:31 (UTC +03:00)
View GitHub Profile
/**
* Executor which useful for unit testing
*/
public class CurrentThreadExecutor implements ExecutorService {
@Override
public void execute(Runnable command) {
command.run();
}
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);
<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>
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / NaiveRateLimiter.java
Created March 21, 2017 11:04
Naive solution for rate limiting. Please use bucket4j instead.
import java.util.LinkedList;
/**
* The naive solution for rate limiter which potentially leads to crash JVM with out of memory error.
*/
public class NaiveRateLimiter {
private long availableTokens;
private final long periodMillis;
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / HystrixRunInCallerThread.java
Created March 30, 2017 14:22
Example of running hystrix command in current thread instead of dedicated thread pool
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
public class HystrixRunInCallerThread {
static HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
.withExecutionIsolationSemaphoreMaxConcurrentRequests(10)
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / GracefullyStoppingScheduledFuture.java
Created November 8, 2017 05:58
Example describes how to cancel ShceduledFuture and wait for runnable to stop, even when runnable is in progress at the moment of cancellation.
public class GracefullyStoppingScheduledFuture {
public static GracefullyStoppingScheduledFuture cheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit, ScheduledExecutorService scheduler) {
CancellableCommand cancellableCommand = new CancellableCommand(command);
ScheduledFuture future = scheduler.scheduleAtFixedRate(cancellableCommand, initialDelay, period, unit);
return new GracefullyStoppingScheduledFuture(future, cancellableCommand);
}
private GracefullyStoppingScheduledFuture(ScheduledFuture targetFuture, CancellableCommand command) {
this.targetFuture = targetFuture;
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / CachingSupplier.java
Last active April 21, 2022 07:09
Example of StamedLock usage
import java.time.Duration;
import java.util.concurrent.locks.StampedLock;
import java.util.function.Supplier;
public class CachingSupplier<T> implements Supplier<T> {
private final Supplier<T> targetSupplier;
private final long cachingDurationNanos;
private final StampedLock stampedLock = new StampedLock();
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / ThreadCreationProfilerAgent.java
Last active December 12, 2019 13:39
Profiler for detecting new thread creation
package com.ringcentral.tread_creation_profiler;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.dynamic.loading.ClassInjector;
import net.bytebuddy.matcher.ElementMatchers;
public class SomeWrapper {
private final long nativeAddress;
public SomeWrapper(long nativeAddress) {
this.nativeAddress = nativeAddress;
}
protected void finalize() throws Throwable {
deleteNativeObject0();
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / AutoExpandingConcurrentArray.java
Created February 8, 2020 09:36
Concurrent array which expands its size if necessary
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicReferenceArray;
public class AutoExpandingConcurrentArray<T> {