Skip to content

Instantly share code, notes, and snippets.

@americanstone
americanstone / all
Last active February 22, 2016 18:45
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap 101 Template</title>
<!-- Bootstrap -->
<!-- Latest compiled and minified CSS -->
@americanstone
americanstone / gist:93c8e395357858df82c3ccc5742bf41a
Created April 12, 2017 12:03
When running this code with the line commented, the thread is not deamon, so even if your main method has finished, you'll keep on having the console flooded until you stop it with CTRL+C. That is, the JVM will not exit.
public class Spawner {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
System.out.println("I'm still alive");
}
}
});
t.start();
(function(){
var sequence = Promise.resolve();
for(var i = 0; i < 10 ; i++){
const timeout = Math.random() * 1000;
sequence = sequence.then((function(icopy){
return new Promise((resolve, reject) => {setTimeout(() => { console.log(icopy); resolve();}, timeout);});
})(i));
}
})()
@americanstone
americanstone / getResponseSize.js
Last active October 17, 2017 15:49
getResponseSize from url
async function getResponseSize(url) {
const response = await fetch(url);
const reader = response.body.getReader();
let result = await reader.read();
let total = 0;
while (!result.done) {
const value = result.value;
total += value.length;
console.log('Received chunk', value);
@americanstone
americanstone / curl7daysgithubproject
Created August 23, 2017 15:50
The last 7 days github trending projects
curl -G https://api.github.com/search/repositories --data-urlencode "sort=stars" --data-urlencode "order=desc" --data-urlencode "q=language:java" --data-urlencode "q=created:>`date -v-7d '+%Y-%m-%d'`" | jq ".items[0,1,2] | {name, description, language, watchers_count, html_url}"
@americanstone
americanstone / CommonPoolCompletionService.java
Last active January 16, 2018 21:15
CommonPoolCompletionService whenComplete hook vs ExecutorCompletionService done hook
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class CommonPoolCompletionService<V> {
private final BlockingQueue<V> completionQueue;
private final BlockingQueue<CompletableFuture<V>> completionQueue1;
@americanstone
americanstone / OrderedParallelLoading.java
Created October 17, 2017 15:47
A recursive resultless ForkJoinTask. The BiFunction knows how to take a list of feed and context to generate a ordered results. context is shared by all child thread. caller need to make sure the context is thread safe
package com.dealer.modules.cms.promotions.services;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.List;
import java.util.concurrent.RecursiveAction;
import java.util.function.BiFunction;
/***
* A recursive resultless ForkJoinTask. The BiFunction knows how to take a list of feed and context to generate a ordered results.
@americanstone
americanstone / Concat.java
Created January 24, 2018 02:49
Adding two streams, or an extra element to a stream
Stream<Foo> stream = stream1.collect(concat(stream2)).collect(concat(element));
Stream<Foo> stream = stream1
.filter(x -> x!=0)
.collect(concat(stream2))
.filter(x -> x!=1)
.collect(concat(element))
.filter(x -> x!=2);
@americanstone
americanstone / hystrix-force-open.md
Created February 13, 2018 18:26
circuitBreaker.forceOpen

Want to know the behavoir of your app if a Hystrix Circuit is open?

Run with the circuit open

Run your spring cloud app with -Dhystrix.command.<circuitKeyName>.circuitBreaker.forceOpen=true

<circuiktKeyName> is the name of you hystrix circuit. If you used @HystrixCommand it is the name of the method. If you used @HystrixCommand(commandKey="mykey") is is the value of the commandKey attribute.

Dynamically force open the circuit

@americanstone
americanstone / OrderedParallelLoading2.java
Last active February 19, 2018 14:37
OrderedParallelLoading BiFunction process list
package com.dealer.modules.cms.promotions.services;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.RecursiveAction;
import java.util.function.BiFunction;
/***