Threading Services

One of the goals of the Liberty core is to have excellent scaling characteristics in multi-core environments. Given the current industry trends, it is clear that single thread performance gains have been minor compared with past improvements. It is also clear that the number of hardware threads supported on newer machines has been increasing rapidly.

While in the past we would have expected single thread performance to roughly double every 18 months, now we expect the number of hardware threads to roughly double every 18 months. With that in mind, Liberty must provide easy to use services that allow users to schedule multiple tasks for parallel execution.

Work Execution Services Provided by Liberty

In order to prevent the proliferation of code to manage threads, Liberty has provided a reference to a java.util.concurrent.ExecutorService implementation in the OSGi Service Registry. The implementation of the employs a work stealing scheduler and automatic tuning that attempts to provide optimal throughput based on the runtime environment and current load.

Since the executor is exposed in the service registry, users can acquire the service reference using any of the supported OSGi mechanisms. One of the simplest ways to do this is to use OSGi Declarative Services to define a component that has a reference to a java.util.concurrent.ExecutorService.

<?xml version='1.0' encoding='utf-8'?> <scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name='executor.user.component'> <implementation class='com.example.threading.ExecutorServiceUser'/> <reference name='executorServiceReference' interface='java.util.concurrent.ExecutorService' bind='setExecutorService' unbind='unsetExecutorService' /> </scr:component>

Using the Executor Service

The ExecutorService interface defined by Java allows users to request execution of multiple tasks in various ways. The most basic of these is submission of an object that implements the Callable interface.

A Callable is an object that declares a call method that performs some action and returns a result. When a Callable is submitted to an executor, the executor returns a handle to the scheduled work known as a Future. The Future instance can then be used to check the status of the work and get the result.

... ExecutorService executor = ... ; MyCallable<Result> callable = new MyCallable<Result>(...); Future<Result> future = executor.submit(callable); ... // Do something else while the callable runs ... // Wait up to 1 second for the callable to complete Result callableResult = future.get(1, TimeUnit.SECONDS); ...
Please see the java ExecutorService documentation for additional examples.

Configuration

There is currently no configuration required for the threading services.