java.lang.Object
java.lang.Thread
net.i2p.util.I2PThread
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
CountryLookup.LookupThread, Elg2KeyFactory, I2PAppThread, JobQueueRunner, MLKEMKeyFactory, PeerManager.ReorgThread, X25519KeyFactory

public class I2PThreadextends 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).
  • Field Details

  • Constructor Details

    • I2PThread

      public I2PThread()
      I2PThread.
    • I2PThread

      public I2PThread(String name)
      I2PThread.
      Parameters:
      name - the thread name shown in logs and thread dumps
    • I2PThread

      public I2PThread(Runnable r)
      I2PThread.
      Parameters:
      r - the body the new thread runs
    • I2PThread

      public I2PThread(Runnable r, String name)
      I2PThread.
      Parameters:
      r - the body the new thread runs
      name - the thread name shown in logs and thread dumps
    • I2PThread

      public I2PThread(Runnable r, String name, boolean isDaemon)
      I2PThread.
      Parameters:
      r - the body the new thread runs
      name - the thread name shown in logs and thread dumps
      isDaemon - true lets the JVM exit without waiting for this thread
    • I2PThread

      public I2PThread(ThreadGroup g, Runnable r)
      I2PThread.
      Parameters:
      g - the thread group the new thread joins, which decides its default priority
      r - the body the new thread runs
    • I2PThread

      public I2PThread(ThreadGroup group, Runnable r, String name)
      Create a named thread in the given group, at normal priority.
      Parameters:
      group - the thread group the new thread joins
      r - the body the new thread runs
      name - the thread name shown in logs and thread dumps
  • Method Details

    • 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
    • run

      public void run()
      Run the thread, catching and reporting OOM and other failures.
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • fireOOM

      protected void fireOOM(OutOfMemoryError oom)
      Notify listeners that an OutOfMemoryError occurred.
      Parameters:
      oom - the error passed to every registered listener
    • addOOMEventListener

      public static void addOOMEventListener(I2PThread.OOMEventListener lsnr)
      Register a new component that wants notification of OOM events.
      Parameters:
      lsnr - the listener to add, ignored if already registered
    • removeOOMEventListener

      public static void removeOOMEventListener(I2PThread.OOMEventListener lsnr)
      Unregister a component that wants notification of OOM events.
      Parameters:
      lsnr - the listener to drop, a no-op if it is not registered