# Class WorkerService

java.lang.Object
org.apache.zookeeper.server.WorkerService

public class WorkerService extends Object
WorkerService is a worker thread pool for running tasks and is implemented using one or more ExecutorServices. A WorkerService can support assignable threads, which it does by creating N separate single thread ExecutorServices, or non-assignable threads, which it does by creating a single N-thread ExecutorService. - NIOServerCnxnFactory uses a non-assignable WorkerService because the socket IO requests are order independent and allowing the ExecutorService to handle thread assignment gives optimal performance. - CommitProcessor uses an assignable WorkerService because requests for a given session must be processed in order. ExecutorService provides queue management and thread restarting, so it's useful even with a single thread.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static class`
`WorkerService.WorkRequest`
Callers should implement a class extending WorkRequest in order to schedule work with the service.

## Constructor Summary

Constructors
Constructor
Description
`WorkerService(String name, int numThreads, boolean useAssignableThreads)`

## Method Summary

Modifier and Type
Method
Description
`void`
`join(long shutdownTimeoutMS)`
`void`
`schedule(WorkerService.WorkRequest workRequest)`
Schedule work to be done.
`void`
`schedule(WorkerService.WorkRequest workRequest, long id)`
Schedule work to be done by the thread assigned to this id.
`void`
`start()`
`void`
`stop()`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### WorkerService

public WorkerService(String name, int numThreads, boolean useAssignableThreads)

Parameters:
  `name` - worker threads are named <name>Thread-##
  `numThreads` - number of worker threads (0 - N) If 0, scheduled work is run immediately by the calling thread.
  `useAssignableThreads` - whether the worker threads should be individually assignable or not

## Method Details

### schedule

public void schedule(WorkerService.WorkRequest workRequest)
Schedule work to be done. If a worker thread pool is not being used, work is done directly by this thread. This schedule API is for use with non-assignable WorkerServices. For assignable WorkerServices, will always run on the first thread.

### schedule

public void schedule(WorkerService.WorkRequest workRequest, long id)
Schedule work to be done by the thread assigned to this id. Thread assignment is a single mod operation on the number of threads. If a worker thread pool is not being used, work is done directly by this thread.

### start

public void start()

### stop

public void stop()

### join

public void join(long shutdownTimeoutMS)