net.i2p.util

## Class I2PThread

- java.lang.Object
- - java.lang.Thread
  - - net.i2p.util.I2PThread

- All Implemented Interfaces:
    Runnable
  Direct Known Subclasses:
    Elg2KeyFactory, I2PAppThread, JobQueueRunner, X25519KeyFactory

      public class I2PThread
      extends Thread

  Preferred over `Thread` for all router uses. For applications, `I2PAppThread` is preferred.

  Provides the following features:

  - Logging to wrapper log on unexpected termination in `run()`.
  - Notification of OOM to registered listener (the router), which will cause logging to the wrapper log and a router restart
  - Catching and logging "OOM" caused by thread limit in `start()` with distinct message, and does not call the OOM listener.
  - As of 0.9.21, initialization to NORM_PRIORITY (not the priority of the creating thread).

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `I2PThread.OOMEventListener` Listener for out-of-memory events.

    - ### Nested classes/interfaces inherited from class java.lang.Thread

      `Thread.State, Thread.UncaughtExceptionHandler`

  - ### Field Summary

    - ### Fields inherited from class java.lang.Thread

      `MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `I2PThread()`
    `I2PThread(Runnable r)`
    `I2PThread(Runnable r, String name)`
    `I2PThread(Runnable r, String name, boolean isDaemon)`
    `I2PThread(String name)`
    `I2PThread(ThreadGroup g, Runnable r)`
    `I2PThread(ThreadGroup group, Runnable r, String name)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `addOOMEventListener(I2PThread.OOMEventListener lsnr)` register a new component that wants notification of OOM events
    `protected void` | `fireOOM(OutOfMemoryError oom)`
    `static void` | `removeOOMEventListener(I2PThread.OOMEventListener lsnr)` unregister a component that wants notification of OOM events
    `void` | `run()`
    `void` | `start()` Overridden to provide useful info to users on OOM, and to prevent shutting down the whole JVM for what is most likely not a heap issue.

    - ### Methods inherited from class java.lang.Thread

      `activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield`

    - ### Methods inherited from class java.lang.Object

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

- - ### Constructor Detail

    - #### I2PThread

          public I2PThread()

    - #### I2PThread

          public I2PThread(Runnable r)

    - #### I2PThread

          public I2PThread(Runnable r,
                           String name)

    - #### I2PThread

          public I2PThread(Runnable r,
                           String name,
                           boolean isDaemon)

    - #### I2PThread

          public I2PThread(String name)

    - #### I2PThread

          public I2PThread(ThreadGroup g,
                           Runnable r)

    - #### I2PThread

          public I2PThread(ThreadGroup group,
                           Runnable r,
                           String name)

  - ### Method Detail

    - #### addOOMEventListener

          public static void addOOMEventListener(I2PThread.OOMEventListener lsnr)

      register a new component that wants notification of OOM events

    - #### fireOOM

          protected void fireOOM(OutOfMemoryError oom)

    - #### removeOOMEventListener

          public static void removeOOMEventListener(I2PThread.OOMEventListener lsnr)

      unregister a component that wants notification of OOM events

    - #### run

          public void run()

      Specified by:
        `run` in interface `Runnable`
      Overrides:
        `run` in class `Thread`

    - #### start

          public void start()

      Overridden to provide useful info to users on OOM, and to prevent shutting down the whole JVM for what is most likely not a heap issue. If the calling thread is an I2PThread an OOM would shut down the JVM. Telling the user to increase the heap size may make the problem worse. We may be able to continue without this thread, particularly in app context.
      Overrides:
        `start` in class `Thread`