Class ControlledThreadProfiler


  • public class ControlledThreadProfiler
    extends Profiler
    This class functions very similar to the Profiler. The difference between the two is while the Profiler profiles all running threads on the VM. This implementation only profiles threads which you explicitly add to be profiled.

    It can be useful to use a ConfigurableThreadFactory where addProfiledThread(Thread) is provided as the Consumer on thread creation.

    Since:
    1.0.0
    • Constructor Detail

      • ControlledThreadProfiler

        public ControlledThreadProfiler()
        Constructs a new profiler instance. The only way to get results from this instance is to call #dump() with a provided output stream to get the results to.
      • ControlledThreadProfiler

        public ControlledThreadProfiler​(int pollIntervalInMs)
        Constructs a new profiler instance. The only way to get results from this instance is to call #dump() with a provided output stream to get the results to.
        Parameters:
        pollIntervalInMs - frequency to check running threads
      • ControlledThreadProfiler

        public ControlledThreadProfiler​(int pollIntervalInMs,
                                        java.util.function.Function<? super Profiler,​java.lang.String> startFutureResultSupplier)
        Constructs a new profiler instance. The only way to get results from this instance is to call #dump() with a provided output stream to get the results to.

        This constructor allows you to change the behavior of the ListenableFuture result when Profiler.start(long) or Profiler.start(Executor, long) is used. Generally this will provide the complete result of Profiler.dump(). This can be replaced with calling Profiler.dump(OutputStream, boolean, int) with parameters to reduce the output, or even null so long as the consumers of the future can handle a null result.

        Parameters:
        pollIntervalInMs - frequency to check running threads
        startFutureResultSupplier - Supplier to be used for providing future results
    • Method Detail

      • addProfiledThread

        public void addProfiledThread​(java.lang.Thread t)
        Adds a thread to be checked by the running profiler.

        If the thread is already included, or if the thread is null, this is a no-op.

        Parameters:
        t - Thread to add to the list of tracked threads
      • removeProfiledThread

        public boolean removeProfiledThread​(java.lang.Thread t)
        Removed a thread from the set of tracked threads. It is good practice to remove a thread from the profiler if it is no longer alive. This profiler makes NO attempt to automatically remove dead threads.
        Parameters:
        t - Thread to remove from tracked set
        Returns:
        true if the thread was found and removed.
      • getProfiledThreadCount

        public int getProfiledThreadCount()
        Call to check how many threads are currently being checked by the profiler. Keep in mind that threads that are not alive may be skipped by the profiler, but will be included in this count.
        Returns:
        count of tracked threads.