Skip to content

Instantly share code, notes, and snippets.

@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();
/**
@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 / 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 / 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 / gist:fe70f5ef8abb4697d61d
Last active August 29, 2015 14:22
Set the JDK version on OSX
# Usage setjdk 1.8
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
fi
:cas (timeout 5000 (assoc op :type :info :value :timed-out)
(let [cas (reify Function
(apply [this, tr]
(let [
[value value'] (:value op)
key_var (byte-array (map byte key))
old_value (.getLong (Tuple/fromBytes (.get (.get tr (byte-array (map byte key))))) 0)
new_value (.pack (Tuple/from (into-array [value'])))]
(if (= old_value value)
(do
(deftest create-cas-test
(let [test (run!
(assoc
noop-test
:name "foundationdb"
:os ubuntu/os
:db db
:client (cas-register)
:model (model/cas-register)
:checker (checker/compose {:html timeline/html
@jhalterman
jhalterman / try-until-success.clj
Created November 20, 2015 00:12
Try until success
(defn try-until-success
"Accepts a try-fn to try a failure-fn to call upon failure, supplying the failure/exception. The try-fn is retried
until no failure is thrown."
[try-fn failure-fn]
(loop []
(if-let [result (try
(try-fn)
(catch Exception e
(failure-fn e)
nil))]
@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;