view src/main/java/org/openjdk/gcbench/roots/ReentrantLocks.java @ 87:f5ac05188aa1

Autogenerate class roots tests
author shade
date Wed, 22 Nov 2017 15:52:08 +0100
parents src/main/java/org/openjdk/gcbench/roots/Synchronizers.java@7b87f91c5db9
children 583fef4276f5
line wrap: on
line source

package org.openjdk.gcbench.roots;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Threads(Threads.MAX)
@State(Scope.Thread)
public class ReentrantLocks {

    List<ReentrantLock> list;

    @Param({"40000"})
    private int size;

    @Setup
    public void setup() {
        list = new ArrayList<>();
        for (int c = 0; c < size; c++) {
            list.add(new ReentrantLock());
        }
    }

    @Benchmark
    public void test(Blackhole bh) throws InterruptedException {
        for (ReentrantLock lock : list) {
            lock.lock();
        }
        bh.consume(new byte[100000]);
        for (ReentrantLock lock : list) {
            lock.newCondition().await(1, TimeUnit.NANOSECONDS);
            lock.unlock();
        }
    }

}