Over a million developers have joined DZone.

A New High Throughput Java Executor Service

A benchmark test between the vmlens and JDK executor services.

· Java Zone

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

The vmlens executor service is a high throughput executor service. It achieves three times higher throughput than the standard JDK executor service. The tradeoff is that the latency is much higher than that of the standard JDK executor service. Here are my benchmark results:

throughput of vmlens executor service

The figure shows the throughput of the vmlens executor Service compared to the standard JDK executor service for different threads on JDK 8.

latency of vmlens executor service

The figure shows the latency of the vmlens executor Service compared to the standard JDK executor service for different threads on JDK 8. Both benchmarks were run with jmh on an Intel i5 4-core CPU. The source of the benchmark can be downloaded here.

A Wait-Free Algorithm for Writing

The basic Idea is that writing should be as fast as possible. The vmlens executor Service uses a thread local field to store its last written queue node. So writing consists of creating a new QueueNode, writing to a thread local field and writing to the volatile QueueNode next field.

public class QueueManyWriters
private final ThreadLocal lastWrittenQueueNode
= new ThreadLocal();

public void accept(E element)
{

	if( dispatcherThread.stop )
	{
		throw new RejectedExecutionException();
	}

	QueueNode current = new QueueNode(element);

	if( lastWrittenQueueNode.get() == null )
	{
		writingThreads.append(current,Thread.currentThread().getId());
		lastWrittenQueueNode.set(new LastWrittenQueueNode(current));
	}
	else
	{

		lastWrittenQueueNode.get().last.next = current;
		lastWrittenQueueNode.get().last = current;

	}
}

...
}
public class QueueNode {

	final E element;
	volatile QueueNode next;

	public QueueNode(E element) 
    {
		super();
		this.element = element;
	}

}

Reading is done by a single dispatcher Thread. The dispatcher collects the tasks and pushes them to one of the worker thread. For collecting the tasks the dispatcher thread uses a local list. The list contains the last read element for each writing thread.

Conclusion

The wait-free algorithm for writing leads to a three time higher throughput than the standard JDK executor service. The vmlens executor service is used in vmlens, a tool to test multithreaded application, for asynchronously writing events to disk.

Whatever type of execution service you use you should test the multithreaded part of your application. Read more about testing multithreaded java code here.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
java ,thread pools

Published at DZone with permission of Thomas Krieger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}