Skip to content

Instantly share code, notes, and snippets.

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 / 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();
}
}
}
@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 / 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)