Skip to content

Instantly share code, notes, and snippets.

@jhalterman
jhalterman / CancellableFuture.scala
Last active January 30, 2022 12:01
A cancellable Scala Future
package net.jodah.cancellablefuture
import java.util.concurrent.atomic.AtomicBoolean
import scala.concurrent._
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success, Try}
object CancellableFuture {
def apply[T](body: => T)(implicit executor: ExecutionContext): CancellableFuture[T] = new CancellableFuture(body)
@jhalterman
jhalterman / MovingAverage.java
Last active January 6, 2019 12:12
An exponentially weighted moving average implementation that decays based on the elapsed time since the last update, approximating a time windowed moving average.
/**
* An exponentially weighted moving average implementation that decays based on the elapsed time since the last update,
* approximating a time windowed moving average.
*/
public class MovingAverage {
private final long windowNanos;
// Mutable state
private volatile long lastNanos;
private volatile double average;
@jhalterman
jhalterman / gist:904e8aba0f38797c4bd7
Created April 17, 2015 00:03
Double-checked lock pattern in Java 8
public static <T> T getWithDcl(Supplier<T> supplier, Supplier<T> factory, Object mutex) {
T object = supplier.get();
if (object == null) {
synchronized (mutex) {
object = supplier.get();
if (object == null) {
object = factory.get();
}
}
}

Keybase proof

I hereby claim:

  • I am jhalterman on github.
  • I am jhalterman (https://keybase.io/jhalterman) on keybase.
  • I have a public key whose fingerprint is 43F6 97A7 C494 AD19 CFCA 48F4 489D 4D97 977C 14CE

To claim this, I am signing this object:

@jhalterman
jhalterman / minscalaactors.scala
Created November 15, 2017 03:27 — forked from viktorklang/minscalaactors.scala
Minimalist Scala Actors
/*
Copyright 2012 Viktor Klang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
@jhalterman
jhalterman / ThreadAwareExceptionTest.java
Last active May 27, 2017 03:54
Experiments in thread aware exceptions
import java.util.Arrays;
public class ThreadAwareExceptionTest {
static class ThreadAwareThread extends Thread {
Throwable entryPoint;
ThreadAwareThread(Runnable runnable) {
super(runnable);
}
@jhalterman
jhalterman / gist:9814439
Last active January 5, 2016 09:52
AbstractResourceTest for testing resources in Dropwizard with TestNG
import io.dropwizard.Configuration;
import io.dropwizard.configuration.ConfigurationFactory;
import io.dropwizard.jackson.Jackson;
import io.dropwizard.jersey.DropwizardResourceConfig;
import io.dropwizard.jersey.jackson.JacksonMessageBodyProvider;
import io.dropwizard.logging.LoggingFactory;
import io.dropwizard.setup.Environment;
import java.io.File;
import java.io.FileNotFoundException;
@jhalterman
jhalterman / gist:9814465
Last active January 5, 2016 09:52
AbstractAppTest for testing Dropwizard Applications with TestNG. This implementation creates a single application for the entire test suite.
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.cli.ServerCommand;
import io.dropwizard.lifecycle.ServerLifecycleListener;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.testing.junit.ConfigOverride;
import java.util.Enumeration;
@jhalterman
jhalterman / InterruptableWaiter.java
Created November 4, 2013 21:02
A waiter where waiting threads can be interrupted (as opposed to awakened).
/**
* A waiter where waiting threads can be interrupted (as opposed to awakened).
*
* @author Jonathan Halterman
*/
public class InterruptableWaiter {
private final Sync sync = new Sync();
private static final class Sync extends AbstractQueuedSynchronizer {
private static final long serialVersionUID = 4016766900138538852L;
@jhalterman
jhalterman / ReentrantCircuit.java
Last active December 27, 2015 10:09
A circuit that accepts re-entrant open and close calls, allows waiting threads to be interrupted, and ensures fairness when releasing threads.
/**
* A circuit that accepts re-entrant {@link #open()} and {@link #close()} calls and ensures fairness
* when releasing {@link #await() waiting} threads.
*
* @author Jonathan Halterman
*/
public class ReentrantCircuit {
private final Sync sync = new Sync();
/**