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
  • 13:58 (UTC +03:00)
View GitHub Profile
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / BatchHelper.java
Created October 30, 2020 08:55
BatchHelper is util that helps to combine parallel requests to the batches. Both synchronous and synchronous styles are supported.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import static java.util.Objects.requireNonNull;
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / Example of Usage
Last active July 10, 2020 10:25
/proc/PID/smaps Parser
public class SProcMapParserExample {
private static InputStream stream = SProcMapParserExample.class.getResourceAsStream("/some_dump_of_proc_maps.txt");
public static class AllStat {
public static void main(String[] args) throws IOException {
SProcMap map = ProcSMapsParser.parse(stream);
List<ProcSMapItem> items = map.getItems();
Collections.sort(items, Comparator.comparingLong(item -> item.getAttributes().get("Rss")));
Collections.reverse(items);
@vladimir-bukhtoyarov
vladimir-bukhtoyarov / MSQueue.java
Created March 13, 2020 16:41
An example of classic Michael-Scott queue implementation in java
import java.util.concurrent.atomic.AtomicReference;
/**
* An example of classic Michael-Scott queue
*
* @param <T>
*/
public class MSQueue<T> {
private final class Node<T> {
@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> {
public class SomeWrapper {
private final long nativeAddress;
public SomeWrapper(long nativeAddress) {
this.nativeAddress = nativeAddress;
}
protected void finalize() throws Throwable {
deleteNativeObject0();
@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;
@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 / 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 / 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 / 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;