Skip to content

Instantly share code, notes, and snippets.

View jonatan-ivanov's full-sized avatar
🦉

Jonatan Ivanov jonatan-ivanov

🦉
View GitHub Profile
@jonatan-ivanov
jonatan-ivanov / CpuMetricsDemo.java
Last active January 29, 2022 05:36
CPU Metrics Demo
package org.example;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
@jonatan-ivanov
jonatan-ivanov / GaugeUpdateTrick.java
Last active February 4, 2022 18:42
Trick for updating a gauge less frequently
// from Jon Schneider
public class GaugeUpdateTrick {
public static void main(String[] args) {
AtomicLong lastCalculatedTime = new AtomicLong(0);
AtomicLong gaugeValue = new AtomicLong(0);
Gauge.builder("expensive", () -> {
long curr = System.nanoTime();
return gaugeValue.updateAndGet(old ->
lastCalculatedTime.updateAndGet(ts -> curr - ts > 10_000 ? curr : ts) == curr ? 0L /* new */ : old);
});
@jonatan-ivanov
jonatan-ivanov / ForkJoinPoolDemo.java
Created December 4, 2024 02:35
Playground for ForkJoinPool, not for prod
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
public class ForkJoinPoolDemo {
private static final int SUBMISSIONS = 10;
private static final int FORKS = 24;
private static final int PARALLELISM = 5;