Skip to content

Instantly share code, notes, and snippets.

@sw1nn
Created April 29, 2013 17:12
Show Gist options
  • Save sw1nn/5483097 to your computer and use it in GitHub Desktop.
Save sw1nn/5483097 to your computer and use it in GitHub Desktop.
package cemerick;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
@SuppressWarnings({"rawtypes", "unchecked"})
public class burn implements Runnable {
public static class PaddedLong {
private volatile long p1 = 7L, p2 = 7L, p3 = 7L, p4 = 7L, p5 = 7L, p6 = 7L, p7 = 7L;
public volatile long value = 0;
private volatile long q1 = 7L, q2 = 7L, q3 = 7L, q4 = 7L, q5 = 7L, q6 = 7L, q7 = 7L;
public PaddedLong (long value) {this.value=value;}
/**
* Here to help make sure false sharing prevention padding is not optimised away.
*
* @return sum of padding.
*/
public long sumPaddingToPreventOptimisation()
{
return p1 + p2 + p3 + p4 + p5 + p6 + p7 + value + q1 + q2 + q3 + q4 + q5 + q6 + q7;
}
}
private static final long k = 5000;
public static LinkedList reverse (LinkedList ll) {
LinkedList rev = new LinkedList();
for (Iterator iter = ll.iterator(); iter.hasNext(); ) {
rev.addFirst(iter.next());
}
return rev;
}
public void run () {
LinkedList value = new LinkedList();
for (long i = 0; i < k; i++) {
value.addFirst(new PaddedLong((long)(i * ((float)i + (i - ((float)i / (i + 1)))))));
}
LinkedList<LinkedList> returnValue = new LinkedList<LinkedList>();
for (long i = 0; i < k; i++) {
returnValue.add(value = reverse(value));
}
System.out.println("This would be the return value: " + returnValue.getLast().size());
}
public static void main(String[] args) throws InterruptedException {
long t = System.currentTimeMillis();
int threads = Integer.parseInt(args[0]);
ThreadPoolExecutor e = (ThreadPoolExecutor)Executors.newFixedThreadPool(threads);
for (int i = 0; i < 8; i++) e.execute(new burn());
e.shutdown();
e.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
System.out.println("time: " + (System.currentTimeMillis() - t));
}
}
/*
(defn burn
([] (loop [i 0
value '()]
(if (>= i 10000)
(count (last (take 10000 (iterate reverse value))))
(recur (inc i)
(cons
(* (int i)
(+ (float i)
(- (int i)
(/ (float i)
(inc (int i))))))
value)))))
([_] (burn)))
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment