Class Threads


  • public class Threads
    extends Object
    Utilities related to threads and threading.
    Author:
    Randall Hauch
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
    • Constructor Detail

      • Threads

        private Threads()
    • Method Detail

      • timer

        public static Threads.Timer timer​(Clock clock,
                                          Duration time)
        Obtain a Threads.Timer that uses the given clock to indicate that a pre-defined time period expired.
        Parameters:
        clock - the clock; may not be null
        time - a time interval to expire
        Returns:
        the Threads.Timer object; never null
      • interruptAfterTimeout

        public static Thread interruptAfterTimeout​(String threadName,
                                                   long timeout,
                                                   TimeUnit timeoutUnit,
                                                   Threads.TimeSince elapsedTimer)
        Create a thread that will interrupt the calling thread when the elapsed time has exceeded the specified amount. The supplied Threads.TimeSince object will be reset when the new thread is started, and should also be reset any time the elapsed time should be reset to 0.
        Parameters:
        threadName - the name of the new thread; may not be null
        timeout - the maximum amount of time that can elapse before the thread is interrupted; must be positive
        timeoutUnit - the unit for timeout; may not be null
        elapsedTimer - the component used to measure the elapsed time; may not be null
        Returns:
        the new thread that has not yet been started; never null
      • interruptAfterTimeout

        public static Thread interruptAfterTimeout​(String threadName,
                                                   long timeout,
                                                   TimeUnit timeoutUnit,
                                                   Threads.TimeSince elapsedTimer,
                                                   Thread threadToInterrupt)
        Create a thread that will interrupt the given thread when the elapsed time has exceeded the specified amount. The supplied Threads.TimeSince object will be reset when the new thread is started, and should also be reset any time the elapsed time should be reset to 0.
        Parameters:
        threadName - the name of the new thread; may not be null
        timeout - the maximum amount of time that can elapse before the thread is interrupted; must be positive
        timeoutUnit - the unit for timeout; may not be null
        elapsedTimer - the component used to measure the elapsed time; may not be null
        threadToInterrupt - the thread that should be interrupted upon timeout; may not be null
        Returns:
        the new thread that has not yet been started; never null
      • timeout

        public static Thread timeout​(String threadName,
                                     long timeout,
                                     TimeUnit timeoutUnit,
                                     Threads.TimeSince elapsedTimer,
                                     Runnable uponTimeout)
        Create a thread that will call the supplied function when the elapsed time has exceeded the specified amount. The supplied Threads.TimeSince object will be reset when the new thread is started, and should also be reset any time the elapsed time should be reset to 0.

        The thread checks the elapsed time every 100 milliseconds.

        Parameters:
        threadName - the name of the new thread; may not be null
        timeout - the maximum amount of time that can elapse before the thread is interrupted; must be positive
        timeoutUnit - the unit for timeout; may not be null
        elapsedTimer - the component used to measure the elapsed time; may not be null
        uponTimeout - the function to be called when the maximum amount of time has elapsed; may not be null
        Returns:
        the new thread that has not yet been started; never null
      • timeout

        public static Thread timeout​(String threadName,
                                     long timeout,
                                     TimeUnit timeoutUnit,
                                     long sleepInterval,
                                     TimeUnit sleepUnit,
                                     Threads.TimeSince elapsedTimer,
                                     Runnable uponTimeout)
        Create a thread that will call the supplied function when the elapsed time has exceeded the specified amount. The supplied Threads.TimeSince object will be reset when the new thread is started, and should also be reset any time the elapsed time should be reset to 0.

        The thread checks the elapsed time every 100 milliseconds.

        Parameters:
        threadName - the name of the new thread; may not be null
        timeout - the maximum amount of time that can elapse before the thread is interrupted; must be positive
        timeoutUnit - the unit for timeout; may not be null
        sleepInterval - the amount of time for the new thread to sleep after checking the elapsed time; must be positive
        sleepUnit - the unit for sleepInterval; may not be null
        elapsedTimer - the component used to measure the elapsed time; may not be null
        uponTimeout - the function to be called when the maximum amount of time has elapsed; may not be null
        Returns:
        the new thread that has not yet been started; never null
      • timeout

        public static Thread timeout​(String threadName,
                                     long timeout,
                                     TimeUnit timeoutUnit,
                                     long sleepInterval,
                                     TimeUnit sleepUnit,
                                     LongSupplier elapsedTime,
                                     Runnable uponStart,
                                     Runnable uponTimeout)
        Create a thread that will call the supplied function when the elapsed time has exceeded the specified amount.
        Parameters:
        threadName - the name of the new thread; may not be null
        timeout - the maximum amount of time that can elapse before the thread is interrupted; must be positive
        timeoutUnit - the unit for timeout; may not be null
        sleepInterval - the amount of time for the new thread to sleep after checking the elapsed time; must be positive
        sleepUnit - the unit for sleepInterval; may not be null
        elapsedTime - the function that returns the total elapsed time; may not be null
        uponStart - the function that will be called when the returned thread is started; may be null
        uponTimeout - the function to be called when the maximum amount of time has elapsed; may not be null
        Returns:
        the new thread that has not yet been started; never null
      • threadFactory

        public static ThreadFactory threadFactory​(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector,
                                                  String connectorId,
                                                  String name,
                                                  boolean indexed,
                                                  boolean daemon)
        Returns a thread factory that creates threads conforming to Debezium thread naming pattern debezium-<connector class>-<connector-id>-<thread-name>.
        Parameters:
        connector - - the source connector class
        connectorId - - the identifier to differentiate between connector instances
        name - - the name of the thread
        indexed - - true if the thread name should be appended with an index
        daemon - - true if the thread should be a daemon thread
        Returns:
        the thread factory setting the correct name
      • threadFactory

        public static ThreadFactory threadFactory​(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector,
                                                  String connectorId,
                                                  String name,
                                                  boolean indexed,
                                                  boolean daemon,
                                                  Consumer<Thread> callback)
        Returns a thread factory that creates threads conforming to Debezium thread naming pattern debezium-<connector class>-<connector-id>-<thread-name>.
        Parameters:
        connector - - the source connector class
        connectorId - - the identifier to differentiate between connector instances
        name - - the name of the thread
        indexed - - true if the thread name should be appended with an index
        daemon - - true if the thread should be a daemon thread
        callback - - a callback called on every thread created
        Returns:
        the thread factory setting the correct name
      • newSingleThreadExecutor

        public static ExecutorService newSingleThreadExecutor​(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector,
                                                              String connectorId,
                                                              String name,
                                                              boolean daemon)
      • newFixedThreadPool

        public static ExecutorService newFixedThreadPool​(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector,
                                                         String connectorId,
                                                         String name,
                                                         int threadCount)
      • newSingleThreadExecutor

        public static ExecutorService newSingleThreadExecutor​(Class<? extends org.apache.kafka.connect.source.SourceConnector> connector,
                                                              String connectorId,
                                                              String name)