The Thread Pool Manager service with its thread pooling mechanism optimizes the resource usage for creating threads.
Overview
The concept of thread pools is that to be able to operate with multiple clients in a system with scanty resources, the OSGi framework must highly optimize the speed of the requests' execution. The speed of the requests' processing depends on how the framework manages its memory buffers, thread pools, sockets, and database connections. Frequently, services in a bundle require creating their own threads when activated, but creating a new thread is a time-consuming operation, which slows down the performance of the framework. In addition, there are server services which create multiple threads to serve concurrent client requests. For optimization of resource usage for creating threads developers can benefit from the thread pooling mechanism of the Thread Pool Manager service.
A thread pool is a collection of created threads, put in waiting state. The Thread Pool Manager service maintains a default thread pool whose threads are shared by all bundles in the OSGi framework.
The Thread Pool Manager service uses two pools for thread pool management:
At startup, the Thread Pool Manager service generates a certain number of threads according to a minimum value defined as a VM system property and pushes them in the idle thread pool. Jobs that request execution are attached to threads from this pool and are executed. The number of existing threads can be extended up to a maximum value assigned again as a system property. If the count of requesting jobs exceeds this value, they are temporarily kept in the waiting jobs pool in which they wait for threads to free. If a thread completes its job, the waiting jobs pool is examined and the job that is first in the wait queue is released and attached to the thread without pooling it.
Note that such a mechanism is suitable mainly for short-lived jobs. Otherwise, the waiting jobs pool will extremely grow and the operation of the Thread Pool Manager service will slow down. Another problem appears when processing bad-written Runnable objects (never exiting their run method). In this case, the number of free threads decreases and the number of already running threads will reach the limit, so it may occur that no more threads can be processed.
For more information check the Developer Guide – Thread Pool.
Registering Bundle
This service is registered by Core Threads Bundle.