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)

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