WorkerService (Apache ZooKeeper - Server 3.7.0 API)
Date: 2021-03-17

org.apache.zookeeper.server

## 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 and Description
    --- | ---
    `static class` | `WorkerService.WorkRequest` Callers should implement a class extending WorkRequest in order to schedule work with the service.

  - ### Constructor Summary

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

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and 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 java.lang.Object

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

- - ### Constructor Detail

    - #### 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 Detail

    - #### 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)

Copyright © 2008–2021 The Apache Software Foundation. All rights reserved.