Skip to content

Instantly share code, notes, and snippets.

@ashkrit
ashkrit / gist:4527491
Last active December 11, 2015 01:59
blogs
Sample files for blogs
@ashkrit
ashkrit / JMHSample_01_HelloWorld
Last active December 19, 2015 10:29
Auto generated code by JHM
@Generated("org.openjdk.jmh.processor.internal.GenerateMicroBenchmarkProcessor")
public final class JMHSample_01_HelloWorld {
@Threads(1)
public Result wellHelloThere_Throughput(Loop loop) throws Throwable {
if (!threadId_inited) {
threadId = threadSelector.getAndIncrement();
threadId_inited = true;
}
@ashkrit
ashkrit / DeadCode
Created July 6, 2013 19:06
Dead code sample sing JHM
@GenerateMicroBenchmark
public void baseline() {
// do nothing, this is a baseline
}
@GenerateMicroBenchmark
public void measureWrong() {
// This is wrong: result is not used, and the entire computation is optimized out.
Math.log(x);
}
private double x = Math.PI;
@GenerateMicroBenchmark
public void baseline() {
// do nothing, this is a baseline
}
@GenerateMicroBenchmark
public double measureWrong() {
// This is wrong: the result is provably the same, optimized out.
@ashkrit
ashkrit / CompilerControl.java
Created July 7, 2013 15:07
JHM compile control
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public void target_dontInline() {
// this method was intentionally left blank
}
@CompilerControl(CompilerControl.Mode.INLINE)
public void target_inline() {
// this method was intentionally left blank
}
@ashkrit
ashkrit / Asymmetric.java
Created July 7, 2013 15:25
JMH Asymmetric sample
private AtomicInteger counter;
@Setup
public void up() {
counter = new AtomicInteger();
}
@GenerateMicroBenchmark
@Group("g")
@Threads(8)
private ReentrantLock lock = new ReentrantLock();
private Condition dataChanged = lock.newCondition();
@Override
public void block() throws InterruptedException {
try {
lock.lock();
dataChanged.await();
} finally {
lock.unlock();
private AtomicReferenceArray<Thread> consumerWorker;
@Override
public void block() throws InterruptedException {
boolean parked=false;
for(int index=0;index<consumer;index++)
{
if(consumerWorker.get(index)==null && consumerWorker.compareAndSet(index, null, Thread.currentThread()))
@ashkrit
ashkrit / TaskRejection
Created January 16, 2014 14:06
Task Rejection
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
while(!queue.isEmpty())
{
T element = queue.take();
//Process element.
}