Sending interrupts to the worker threads upon teardown

Chris Vest mr.chrisvest at gmail.com
Mon Apr 14 16:45:46 UTC 2014


Hi,

Say you want to benchmark the put() and take() methods of SynchronousQueue. Your first instinct (or at least my first instinct) might be to write something like this:

@State( Scope.Benchmark )
public class BenchmarkSynchronousQueue
{
    public static final Object TOKEN = new Object();

    private SynchronousQueue<Object> queue;

    @Setup
    public void createQueue()
    {
        queue = new SynchronousQueue<Object>();
    }

    @GenerateMicroBenchmark
    @Group("a")
    @GroupThreads(1)
    public void put() throws InterruptedException
    {
        queue.put( TOKEN );
    }

    @GenerateMicroBenchmark
    @Group("a")
    @GroupThreads(1)
    public Object take() throws InterruptedException
    {
        return queue.take();
    }
}

However, because of the blocking nature of the queue, there’s a pretty high chance that the benchmark will get stuck, because the shutdown of the producer does not exactly line up with the shutdown of the consumer.
The simplest way to fix this, that I can think of, is to send them interrupts, since it is a common expectation of well behaved blocking methods to respond to interrupts.

Is there a good way to do this already, or is it something that could be added in a future version? (Or is my idea bad and I should feel bad?)

Cheers,
Chris



More information about the jmh-dev mailing list